【发布时间】:2012-09-05 20:05:49
【问题描述】:
这是我目前的方法的样子:
// Somewhere in a UI class
// Called when a button called "Start" clicked
MyWindow::OnStartClicked(Event &sender)
{
_thread = new boost::thread(boost::bind(&MyWindow::WorkToDo, this));
}
MyWindow::WorkToDo()
{
for(int i = 1; i < 10000000; i++)
{
int percentage = (int)((float)i / 100000000.f);
_progressBar->SetValue(percentage);
_statusText->SetText("Working... %d%%", percentage);
printf("Pretend to do something useful...\n");
}
}
// Called on every frame
MyWindow::OnUpdate()
{
if(_thread != 0 && _thread->timed_join(boost::posix_time::seconds(0))
{
_progressBar->SetValue(100);
_statusText->SetText("Completed!");
delete _thread;
_thread = 0;
}
}
但我担心这很不安全,因为我在程序执行结束时不断收到未处理的异常。
我基本上想将繁重的任务分离到另一个线程中而不阻塞 GUI 部分。
【问题讨论】:
-
您提供的这段代码应该可以工作。你能以某种方式指定“未处理的异常”吗?
-
我正在使用 SFML2 进行渲染,但异常来自 SFML 的 MutexImpl.cpp。如果详细信息来自另一个库,恐怕对于这个问题来说太具体了。
标签: c++ multithreading boost-thread