Make SafeThread header-only. (#117)

This commit is contained in:
Peter Johnson
2016-09-25 18:21:29 -07:00
committed by GitHub
parent d8ee44349c
commit 760d6a26d3
2 changed files with 21 additions and 31 deletions

View File

@@ -76,6 +76,27 @@ class SafeThreadOwnerBase {
std::atomic<SafeThread*> m_thread;
};
inline void SafeThreadOwnerBase::Start(SafeThread* thr) {
SafeThread* curthr = nullptr;
SafeThread* newthr = thr;
if (!m_thread.compare_exchange_strong(curthr, newthr)) {
delete newthr;
return;
}
std::thread([=]() {
newthr->Main();
delete newthr;
}).detach();
}
inline void SafeThreadOwnerBase::Stop() {
SafeThread* thr = m_thread.exchange(nullptr);
if (!thr) return;
std::lock_guard<std::mutex> lock(thr->m_mutex);
thr->m_active = false;
thr->m_cond.notify_one();
}
} // namespace detail
template <typename T>