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

时间:2014-04-25 13:27:40

标签: c++ linux multithreading boost

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

thread.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的东西,如某些特定于编译器的设置或标记或可能产生此类错误的任何内容。

2 个答案:

答案 0 :(得分:1)

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

但是,我也没有看到陷入僵局的空间。

这是一个独立的版本,可以显示它"工作"的 Live On 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 :(得分:0)

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