//创建线程后, 不用join 和 detach,

//清理时 先判断是否 joinable(), 在 join 或 detach 后 删除

 

//如果是临时变量, 出作用域线程对象会自动析构

std::thread* loot_thrd = nullptr;
loot_thrd = new std::thread(AutoProc);  //线程从这句就开始执行了

//获取对象内部维护的有效本地句柄,  由对象内部自己管理资源,

//有必时,自己 DuplicateHandle 一份
g_loot_thread_handle = loot_thrd->native_handle();

 

std::thread::detach()

//从 thread 对象分离执行线程,允许执行独立地持续。一旦该线程退出,则释放任何分配的资源。

//调用 detach 后 *this 不再占有任何线程, ( detach() 后可以马上 执行 delete() )

//detach() 之后无法获得句柄

//且之前获得 g_loot_thread_handle 句柄也会无效

//loot_thrd.detach();

// 没有 执行 join 和 detach() 的 thread指针 不能 执行 delete()

 

相关文章:

  • 2022-12-23
  • 2021-10-25
  • 2021-11-17
  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
  • 2021-08-06
猜你喜欢
  • 2021-07-09
  • 2021-08-17
  • 2021-06-21
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案