我目前正在开发一个程序,需要从套接字服务器下载一些图片,并且下载工作将会执行很长时间。所以,我创建了一个新的std::thread
来做到这一点。如何终止一个std ::线程?
下载后,std::thread
会调用当前类的成员函数,但该类很可能已经发布。所以,我有一个例外。
如何解决这个问题?
void xxx::fun1()
{
...
}
void xxx::downloadImg()
{
...a long time
if(downloadComplete)
{
this->fun1();
}
}
void xxx::mainProcees()
{
std::thread* th = new thread(mem_fn(&xxx::downloadImg),this);
th->detach();
//if I use th->join(),the UI will be obstructed
}
欢迎来到Stack Overflow!我尽可能地猜测你的问题,然后编辑你的问题。但是,添加对代码和描述的解释,以便更多具有该主题知识的人员可以看到它。如果需要识别特定问题,请编辑您遇到的特定错误消息。祝你好运! – manetsus
为什么不使用'std :: future'? –
Superlokkus
[C++ 0x线程中断]的可能重复(http://stackoverflow.com/questions/2790346/c0x-thread-interruption) – kfsone