vendredi 22 juillet 2016

How to terminate a std::thread

I am currently developing a program that needs to download some images from the socket server,and the downloading work will execute a long time,so I create a new std::thread to do that,Once it's downloaded,the std::thread will call a member function of current Class,but,this Class is likely to have been released,so I got a exception.How to solve this problem?

`

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
}

`

Aucun commentaire:

Enregistrer un commentaire