18 #ifndef FOEDUS_THREAD_STOPPABLE_THREAD_IMPL_HPP_
19 #define FOEDUS_THREAD_STOPPABLE_THREAD_IMPL_HPP_
48 started_(false), stop_requested_(false), stopped_(false) {}
58 std::thread &&the_thread,
const std::chrono::microseconds &sleep_interval);
60 void initialize(
const std::string &name_prefix, int32_t name_ordinal,
61 std::thread &&the_thread,
const std::chrono::microseconds &sleep_interval);
106 return stop_requested_.load(std::memory_order_relaxed);
115 template <
typename HANDLE_TYPE>
116 HANDLE_TYPE
native_handle() {
return static_cast<HANDLE_TYPE
>(thread_.native_handle()); }
127 std::chrono::microseconds sleep_interval_;
131 std::atomic<bool> started_;
133 std::atomic<bool> stop_requested_;
135 std::atomic<bool> stopped_;
141 #endif // FOEDUS_THREAD_STOPPABLE_THREAD_IMPL_HPP_
bool is_stopped() const
returns whether this thread has stopped (if the thread hasn't started, false too).
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
void initialize(const std::string &name, std::thread &&the_thread, const std::chrono::microseconds &sleep_interval)
Initializes this object for the given thread.
void request_stop()
If the thread is still running, requests the thread to stop and waits until it exists.
std::string to_string() const
void stop()
request_stop() plus wait_for_stop().
void wait_for_stop()
Blocks until the thread stops.
StoppableThread & operator=(const StoppableThread &other)=delete
An analogue of pthread's condition variable and std::condition_variable to avoid glibc's bug in pthre...
bool is_stop_requested() const
returns whether someone has requested to stop this.
HANDLE_TYPE native_handle()
wraps std::thread::native_handle()
void wakeup()
If the thread is still running and also sleeping, requests the thread to immediately wakeup and do it...
bool is_stop_requested_weak() const
non-atomic is_stop_requested().
The frequently appearing quartet of std::thread, condition_varible, stop-request flag, and mutex.
friend std::ostream & operator<<(std::ostream &o, const StoppableThread &v)
bool sleep()
Sleep until the interval elapses or someone requests to stop this thread.
bool is_stopped_weak() const
non-atomic is_stopped().