std::lock_guard<std::recursive_mutex> lock(thread_mutex);
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.
}
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!