C++如何触发PostMessage在另一个线程中进行
C++ How can i trigger that a PostMessage was proceed in another thread
我有一个MFC-C++应用程序,其中至少有两个线程正在运行"MainFrame"(=GUI线程)和一个"Solver"线程。
在某一点上,第二个线程(解算器)启动GUI线程应该通过PostMessage(...)
执行的模型更改。为了安全起见,我想等待消息在第二个线程中继续。
通过使用SendMessage(...)
,解算器线程等待消息执行,但通过这种方式,我们绕过了不应该成为目标的消息队列。
我的问题是:在继续之前,我如何正确、干净地检查/触发我的消息是否继续?
- 我们是否需要通过
GetQueueStatus(...)
每隔"x"秒检查一次主线程消息队列的状态 - 有没有办法让主线程向另一个线程发送"成功事件"?第二个在等待一个事件回来
- boost是否为这类问题提供了一个简单的解决方案
- 是不是已经有一个类似的问题我没有发现?(对不起)
我的功能:
void PostSequencerMessageToGUI(ESequencerSignal signal, CSchedulerStep * step)
{
CMainFrame *mainFrame = theApp.GetMainFrame();
assert(mainFrame);
if (!mainFrame)
return;
mainFrame->PostMessage(WM_SEQUENCER_SIGNAL, signal, reinterpret_cast<LPARAM>(step));
// mainFrame->SendMessage(WM_SEQUENCER_SIGNAL, signal, reinterpret_cast<LPARAM>(step));
// Message was posted, now wait for event back to continue procedure
// ... but how? ...and when to cancel ? ...
return;
}
由于SendMessage忽略了当前消息队列(它通过了队列),我无法使用这种方法。
我在另一个问题中找到了我的解决方案,该问题使用条件变量和互斥来解决。https://stackoverflow.com/a/16909012/5036139
我的解决方案:
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
boost::condition_variable g_sequencerJobCondition;
boost::mutex g_guiMutex;
void PostSequencerMessageToGUI(ESequencerSignal signal, CSchedulerStep * step)
{
CMainFrame *mainFrame = theApp.GetMainFrame();
assert(mainFrame);
if (!mainFrame)
return;
bool work_is_done = false;
try {
boost::mutex::scoped_lock lock(g_guiMutex);
mainFrame->PostMessage(WM_SEQUENCER_SIGNAL, reinterpret_cast<WPARAM>(&work_is_done), reinterpret_cast<LPARAM>(step));
// Message was posted, now wait for event back to continue procedure
while (!work_is_done){
g_sequencerJobCondition.wait(lock);
}
}
catch (... /*e*/){
// Handle all kind of exception like boost::thread_resource_error, boost::thread_interrupted, boost::exception, std::exception
// ...
}
// ...
return;
}
和
LRESULT CMainFrame::OnSequencerPostMessage( WPARAM wParam, LPARAM lParam )
{
// ...
bool *work_is_done = reinterpret_cast<bool*>(wParam);
CSchedulerStep* step = reinterpret_cast<CSchedulerStep*>(lParam);
// Handle business case ...
// ...
// Finally notify sequencer thread that the work is done
work_is_done = true;
g_sequencerJobCondition.notify_one();
return true;
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 从C++11中的线程返回一个值
- qt多线程:计算一个积分
- volatile变量在一个客户线程和一个生产者线程中是否安全
- C++ pthread,两个线程读取一个全局变量
- 如何启动一个 boost::线程运行一个成员函数
- 如果另一个线程将一个元素推到向量的末尾,则向量上的迭代器无效
- Qt::线程是一个库还是仅仅是一个类
- 当一个线程锁定一个大映射时,如何避免冻结其他线程
- C++-如何让多个线程写入一个文件
- 为什么我的线程被一个关键部分阻塞,而没有被任何东西抓住
- 一个线程读一个线程写,std::deque是安全的
- 从c++线程发出一个信号
- 通信b/w两个线程在一个公共的数据结构.设计问题
- 如何使用boost运行线程另一个对象函数与回调
- 我可以在OpenMP分配多个线程到一个代码段
- 当我从线程B读取相同的变量时,我能从线程a写入一个变量吗
- 线程中一个套接字发送/接收
- 跨线程发送一个glib信号
- 用一个插入线程和一个读线程映射并发性