C 11:确保通过主线程进行GUI调用
C++11: Ensure that GUI calls are made from the main thread
我有一个多线程GUI程序,我需要在另一个线程中排队的事件。我想确保GUI调用主要是由主线程进行的。调用std::this_thread::get_id()
是否在整个应用程序中保留其值?
我正在计划这样的事情:
GuiElement
{
std::thread::id main_thread_id;
public:
GuiElement()
{
main_thread_id = std::this_thread::get_id();
}
void thread_check()
{
if(std::this_thread::get_id() != this->main_thread_id)
{
throw std::runtime_error("You can't call this from a different thread");
}
}
void remove_element(const std::string& element_name)
{
this->thread_check();
//do stuff
}
};
这是正确的事情吗?还是可以实现此功能的更好的东西?
虽然不太可能,但我担心由于某种原因,线程ID可能会更改。这会发生吗?
对于您有关线程更改ID的可能性的问题,只要线程运行,您就可以在该线程上传递,可以肯定地假设线程ID不会更改。
在这里查看std :: thread :: id,它指出类目的是在关联容器中使用键。
相关文章:
- QtConcurrent - 在发布到 UI 线程的数千个结果中保持 GUI 响应
- 线程和 GUI 处理
- 多线程Windows GUI应用程序中的死锁
- 带GUI的Qt多线程
- 与非主线程的 GUI 线程通信
- Qt 如何将数据从一个 GUI 传递到另一个线程
- Qt:工作线程和 GUI 事件之间的关系
- 解决方法:QPixmap:在GUI线程之外使用pixmap是不安全的
- 处理中断时,如何回到主gui线程
- 从其他std ::线程更新QT GUI
- QNetworkAccessManager即使在另一个线程中也能冻结GUI
- 如何在非 gui 线程上正确创建 QUdpSocket?未发出现读
- Qt的GUI线程在从外部对象调用方法时是否会在引擎盖下生成线程?
- ROS-Qt GUI - 如何分发线程
- 由于线程问题,Qt-Gui没有更新
- 当主GUI线程被阻塞时,如何从工作线程创建无模式对话框
- Qt 停止来自 GUI 线程的工作线程循环
- 使用第二个线程快速更新 GUI 窗口会通过调整窗口大小或移动窗口而崩溃
- 如何降低 QT Gui 线程优先级
- 使用 Qt 的简单线程 gui 应用程序的结构是什么?