qcode
  • 创作工场专业版,一问多答!更智能、更完整、更原创
  • 这是一个超强内容创作平台,它会写文章、做设计、绘图、敲代码,有问必答!
🔥点击使用-高级专业版🔥

std::lock_guard<std::recursive_mutex> lock(thread_mutex);

时间2023-04-02 01:09:47 提问来自 上海市上海市 的网友 分类创作 浏览307
if (thread_state != ThreadState::Running){ return; } // Do the work thread_func(this); // See if we should keep running or exit if (thread_state == ThreadState::Running){ thread_state = ThreadState::Idle; } else { thread_state = ThreadState::Exited; // We're done here! Exit the thread. This is needed for platforms that don't support pthreads join(). Is there a better way to do this? This doesn't seem very safe. :( std::lock_guard<std::recursive_mutex> lock(thread_mutex); // Need to lock in case someone is waiting on us to exit in Join() below. auto l = [](Thread *t) { t->Exit(); }; // Create a lambda function to call our Exit() function so we can use it with notifyOne(). It needs to be outside of the scope of the lock guard so that it can be called when we notify one. thread_cv.notify_one(l, this); // Notify one waiting on us that we are exiting, and pass them our pointer so they can call our Exit() function and clean up after us. return; // We're done here! Exit the thread now! } } while (true); // Keep looping until told otherwise by setting state to something other than Running or Idle. }

点击这里,进行内容创作

创作工场 免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!

上一篇 游戏的发展史 下一篇 提升生手动整理资料能力的预期结果