C ++ Windows MFC并发:让线程等待直到特定状态实现 [英] C++ Windows MFC Concurrency: Get thread to wait until particular state achieved
本文介绍了C ++ Windows MFC并发:让线程等待直到特定状态实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Windows MFC并发,我如何告诉我的当前线程等待直到一个特定的状态被实现?我现在唯一能想到的方法是执行定期睡眠并检查状态 - 当我们处于预期的状态,然后继续。有没有更好的方法这样做?
BOOL achieve = FALSE;
int main(int argc,char ** argv){
//此函数创建一个新线程并在未来的某个时间修改'已实现'全局变量
doSomethingOnAnotherThread();
//对于已达到,等待最大4秒,否则放弃
for(int i = 0; i <5; i ++){
EnterCriticalSection ; CS);
int localAchieved = achie;
LeaveCriticalSection(& CS);
if(!localAchieved){
if(i == 4){
cout< 等了太久了...放弃< endl;
break;
}
Sleep(1000); //让我们等待1秒钟,看看会发生什么
} else {
cout<< 实现为TRUE,恢复主线程< endl;
break;
}
}
}
解决方案>
表示一个事件,它是一个同步对象,可以让一个线程通知另一个线程已发生事件。
因此,这是解决问题的适当工具。
p>
void DoSomethingOnAnotherThread(CEvent * event)
{
//长时间运行。
...
//设置事件的状态以发出信号,释放任何等待的线程。
event-> SetEvent();
// TODO:也许添加try / catch和SetEvent()总是在长时间运行操作后?
}
int main(int argc,char ** argv)
{
//手动复位事件。
CEvent achie_event(FALSE,TRUE);
//此函数创建一个新的线程,并在未来的某个时刻修改'已实现'全局变量
DoSomethingOnAnotherThread(& achieved_event);
//等待事件发出信号4秒钟!
DWORD wait_result = WaitForSingleObject(achieved_event,4000);
switch(wait_result){
case WAIT_OBJECT_0:
std :: cout< 实现! << std :: endl;
break;
case WAIT_TIMEOUT:
std :: cout<< 暂停! << std :: endl;
break;
default:// WAIT_FAILED
std :: cout<< 无法等待! << std :: endl;
break;
}
}
On Windows MFC concurrency, how do I tell my current thread to wait until a particular state is achieved? The only way I can think of at the moment is to perform periodical Sleep and check the state -- when we're at intended state then continue. Is there any better way of doing this?
BOOL achieved = FALSE;
int main (int argc, char** argv) {
// This function creates a new thread and modifies the 'achieved' global variable at some point in the future
doSomethingOnAnotherThread();
// Wait maximum 4 seconds for 'achieved' to be TRUE, otherwise give up
for(int i=0; i<5; i++) {
EnterCriticalSection(&CS);
int localAchieved = achieved;
LeaveCriticalSection(&CS);
if (!localAchieved) {
if(i==4) {
cout << "waited too long .. giving up" << endl;
break;
}
Sleep(1000); // let's wait 1 more second and see what happen
} else {
cout << "achieved is TRUE, resuming main thread" << endl;
break;
}
}
}
解决方案
Represents an event, which is a synchronization object that enables one thread to notify another that an event has occurred.
So, it is the appropriate tool to solve the problem.
Let's illustrate it:
void DoSomethingOnAnotherThread(CEvent* event)
{
// Long-running operation.
...
// Sets the state of the event to signaled, releasing any waiting threads.
event->SetEvent();
// TODO: maybe add try/catch and SetEvent() always after the long-running operation???
}
int main (int argc, char** argv)
{
// Manual-reset event.
CEvent achieved_event(FALSE, TRUE);
// This function creates a new thread and modifies the 'achieved' global variable at some point in the future
DoSomethingOnAnotherThread(&achieved_event);
// Wait the event to be signalled for 4 seconds!
DWORD wait_result = WaitForSingleObject(achieved_event, 4000);
switch (wait_result) {
case WAIT_OBJECT_0:
std::cout << "Achieved!" << std::endl;
break;
case WAIT_TIMEOUT:
std::cout << "Timeout!" << std::endl;
break;
default: // WAIT_FAILED
std::cout << "Failed to wait!" << std::endl;
break;
}
}
这篇关于C ++ Windows MFC并发:让线程等待直到特定状态实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文