-namespace {
-
- // Helpers to launch a thread after creation and joining before delete. Must be
- // outside Thread c'tor and d'tor because the object must be fully initialized
- // when start_routine (and hence virtual idle_loop) is called and when joining.
-
- template<typename T> T* new_thread() {
- std::thread* th = new T;
- *th = std::thread(&T::idle_loop, (T*)th); // Will go to sleep
- return (T*)th;
- }
-
- void delete_thread(ThreadBase* th) {
-
- th->mutex.lock();
- th->exit = true; // Search must be already finished
- th->mutex.unlock();
-
- th->notify_one();
- th->join(); // Wait for thread termination
- delete th;
- }