我目前正在开发一个程序,需要从套接字服务器下载一些图像,下载工作将执行很长时间.所以,我创造了一个新std::thread的做法.
一旦下载,std::thread它将调用当前Class的成员函数,但此类可能已被释放.所以,我有一个例外.
如何解决这个问题呢?
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
}
Run Code Online (Sandbox Code Playgroud)