Linux应用程序在boost::thread::join时冻结

Linux application freeze on boost::thread::join

本文关键字:join 冻结 thread Linux boost 应用程序      更新时间:2023-10-16

我目前正在将我们的c++qt应用程序移植到在MSVC中开发的linux上,它在一些线程操作上一直处于冻结状态。在窗户里,一切都很好。我们的线程实现使用以下代码(摘录):

线程.h:

class our_thread
{
    public:
        our_thread();
        ~our_thread();
        void run(boost::thread*);
        void join(const char* = 0);
        inline bool is_running() const;
    private:
        void delete_thread();
        bool thread_is_running;
        boost::thread* thread_object;
};

thread.cpp:

void our_thread::run(boost::thread* t)
{
    this->delete_thread();
    this->thread_object = t; 
    this->thread_is_running = true;
    QApplication::setOverrideCursor(Qt::BusyCursor);   
    plot("in run");
}
void our_thread::join(const char* text)
{
    plot("in join");
    if(this->thread_object == 0) { return; }
    this->thread_is_running = false;
    QApplication::restoreOverrideCursor();
    if(text)
    {
        plot(text);
    }
    progress.show(false);
    this->thread_object->join();
    this->delete_thread();
}
void our_thread::delete_thread()
{
    this->thread_is_running = false;
    delete this->thread_object;
    this->thread_object = 0;
}

和调用,例如:

this->do_something_thread.run(new boost::thread(&function_name, this, param1));
...
this->do_something_thread.join("Part creation finished");

plot()调用是用户信息,但在这里也用于调试输出。

现在,对于某些操作,这个过程总是有效的,对于一些操作,它总是在线程中的所有代码完成并调用join()之后冻结应用程序。如果我调试软件并逐步完成代码,它会神奇地完美工作,直到我再次尝试使用线程。然后它直接冻结在run()调用上。

我试着提高1.39.0和1.49.0,没有任何区别。我们使用的是RedHat 5.4,所有内容都是用gcc44编译的。

编辑:我想强调的是,在windows上一切都很好,所以我不认为这是一个简单的代码错误。更像是一个特定于linux的东西,比如一些特定于编译器的设置或标志,或者任何可能会产生这种错误的东西。

我不明白为什么要使用复杂的手动内存管理和冗余布尔标志(如果有线程,则必须连接,布尔标志是冗余和未使用的)。

然而,我也不认为有陷入僵局的余地。

这是一个独立的版本,显示它"工作"在Coliru上直播

#include <boost/thread.hpp>
void plot(std::string msg)
{
    static boost::mutex mx;
    boost::lock_guard<boost::mutex> lk(mx);
    std::cout << msg << "n";
}
class our_thread
{
    public:
        our_thread() : thread_object(0), thread_is_running(false) {}
        ~our_thread(){delete_thread();}
        void run(boost::thread*);
        void join(const char* = 0);
        inline bool is_running() const;
    private:
        void delete_thread();
        bool thread_is_running;
        boost::thread* thread_object;
};
void our_thread::run(boost::thread* t)
{
    this->delete_thread();
    this->thread_object = t; 
    this->thread_is_running = true;
    plot("in run");
}
void our_thread::join(const char* text)
{
    plot("in join");
    if(this->thread_object == 0) { return; }
    this->thread_is_running = false;
    if(text)
    {
        plot(text);
    }
    plot("progress.show(false);");
    this->thread_object->join();
    this->delete_thread();
}
void our_thread::delete_thread()
{
    this->thread_is_running = false;
    delete this->thread_object;
    this->thread_object = 0;
}
void function_name(std::string param1)
{
    plot("Enter function_name");
    plot(param1);
    boost::this_thread::sleep_for(boost::chrono::seconds(3));
    plot("Leave function_name");
}
int main()
{
    our_thread do_something_thread;
    do_something_thread.run(new boost::thread(&function_name, "param1"));
    // ...
    do_something_thread.join("Part creation finished");
}

你能算出显示你问题的最小变化吗?然后也许你可以找出原因。

问题是我们从线程本身内部连接线程。在1.54版本中,这抛出了一个异常,告诉我们这样做会导致死锁。这样的异常显然不会在旧版本中抛出(同时也不会因为任何原因导致死锁)。在从线程外部调用联接之后,现在工作正常。