Linux应用程序在boost::thread::join时冻结
Linux application freeze on boost::thread::join
我目前正在将我们的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版本中,这抛出了一个异常,告诉我们这样做会导致死锁。这样的异常显然不会在旧版本中抛出(同时也不会因为任何原因导致死锁)。在从线程外部调用联接之后,现在工作正常。
相关文章:
- 在std::thread中,joinable()然后join()线程安全吗
- WIN32:C++,为什么在WM_CLOSE上调用Messagebox函数程序正在冻结
- Win32 发送输入鼠标移动滞后并冻结
- join() 失败,如果在线程内部调用 io_context.run()
- ImGui 在单击按钮后冻结
- QWidget UI 在使用 QQuickWidget 时冻结
- 如何防止 std::thread 在 QT 中冻结 GUI?
- Adafruit 羽毛RFM69HCW在使用过程中会冻结,需要硬重置
- MFC/C++ ComboBox:禁用下拉列表关闭和打开(UI 冻结)的绘制
- 模板总和函数冻结字符
- 程序在对mouse_event的 Windows API 调用中冻结
- MSYS2 上的 CMake 冻结
- Qt,操作缓慢时不要冻结GUI输入元素
- 延迟函数使打开的框架窗口冻结,直到指定的时间过去
- 在使用PyInstaller冻结Python脚本时包含一个C++可执行文件
- std::future::get()或std::future::wait()是std::thread::join()的替
- 在VS 15.7.5中,悬停在Open CV 3.4.1Mat上仍然冻结
- C++(SFML?激活某些内容x秒钟,而不会冻结整个程序
- c++17 std::thread join() : 没有这样的进程
- Linux应用程序在boost::thread::join时冻结