libfoedus-core
FOEDUS Core Library
|
The frequently appearing quartet of std::thread, condition_varible, stop-request flag, and mutex. More...
The frequently appearing quartet of std::thread, condition_varible, stop-request flag, and mutex.
This quartet helps implement a thread that consumes some task and also occasionally checks if someone requested to stop this thread. As this depends on C++11, the name of this file ends with impl. Thus, only private implementation classes directly use this class. If you are okay with C++11, you can use it from client programs, too.
Definition at line 45 of file stoppable_thread_impl.hpp.
#include <stoppable_thread_impl.hpp>
Public Member Functions | |
StoppableThread () | |
StoppableThread (const StoppableThread &other)=delete | |
StoppableThread & | operator= (const StoppableThread &other)=delete |
StoppableThread (StoppableThread &&other)=delete | |
StoppableThread & | operator= (StoppableThread &&other)=delete |
void | initialize (const std::string &name, std::thread &&the_thread, const std::chrono::microseconds &sleep_interval) |
Initializes this object for the given thread. More... | |
void | initialize (const std::string &name_prefix, int32_t name_ordinal, std::thread &&the_thread, const std::chrono::microseconds &sleep_interval) |
An overload to receive the common pattern of names "Xxx-ordinal". More... | |
void | stop () |
request_stop() plus wait_for_stop(). More... | |
void | request_stop () |
If the thread is still running, requests the thread to stop and waits until it exists. More... | |
void | wait_for_stop () |
Blocks until the thread stops. More... | |
void | wakeup () |
If the thread is still running and also sleeping, requests the thread to immediately wakeup and do its job. More... | |
bool | sleep () |
Sleep until the interval elapses or someone requests to stop this thread. More... | |
bool | is_stop_requested () const |
returns whether someone has requested to stop this. More... | |
bool | is_stop_requested_weak () const |
non-atomic is_stop_requested(). More... | |
bool | is_stopped () const |
returns whether this thread has stopped (if the thread hasn't started, false too). More... | |
bool | is_stopped_weak () const |
non-atomic is_stopped(). More... | |
template<typename HANDLE_TYPE > | |
HANDLE_TYPE | native_handle () |
wraps std::thread::native_handle() More... | |
std::string | to_string () const |
Friends | |
std::ostream & | operator<< (std::ostream &o, const StoppableThread &v) |
|
inline |
Definition at line 47 of file stoppable_thread_impl.hpp.
|
delete |
|
delete |
void foedus::thread::StoppableThread::initialize | ( | const std::string & | name, |
std::thread && | the_thread, | ||
const std::chrono::microseconds & | sleep_interval | ||
) |
Initializes this object for the given thread.
Definition at line 29 of file stoppable_thread_impl.cpp.
Referenced by initialize().
void foedus::thread::StoppableThread::initialize | ( | const std::string & | name_prefix, |
int32_t | name_ordinal, | ||
std::thread && | the_thread, | ||
const std::chrono::microseconds & | sleep_interval | ||
) |
An overload to receive the common pattern of names "Xxx-ordinal".
Definition at line 40 of file stoppable_thread_impl.cpp.
References initialize().
|
inline |
returns whether someone has requested to stop this.
Definition at line 103 of file stoppable_thread_impl.hpp.
Referenced by request_stop(), and sleep().
|
inline |
non-atomic is_stop_requested().
Definition at line 105 of file stoppable_thread_impl.hpp.
|
inline |
returns whether this thread has stopped (if the thread hasn't started, false too).
Definition at line 110 of file stoppable_thread_impl.hpp.
Referenced by request_stop(), and wait_for_stop().
|
inline |
non-atomic is_stopped().
Definition at line 112 of file stoppable_thread_impl.hpp.
|
inline |
wraps std::thread::native_handle()
Definition at line 116 of file stoppable_thread_impl.hpp.
|
delete |
|
delete |
void foedus::thread::StoppableThread::request_stop | ( | ) |
If the thread is still running, requests the thread to stop and waits until it exists.
If the thread has not started or has already stopped, do nothing (so, this is idempotent). Unlike stop(), this method doesn't join the thread. So, it immediately returns.
Definition at line 68 of file stoppable_thread_impl.cpp.
References is_stop_requested(), is_stopped(), and foedus::thread::ConditionVariable::notify_one().
Referenced by stop().
bool foedus::thread::StoppableThread::sleep | ( | ) |
Sleep until the interval elapses or someone requests to stop this thread.
For example, use it as follows.
Definition at line 47 of file stoppable_thread_impl.cpp.
References is_stop_requested(), and foedus::thread::ConditionVariable::wait_for().
void foedus::thread::StoppableThread::stop | ( | ) |
request_stop() plus wait_for_stop().
This method is idempotent. If the thread is not running already, this immediately returns.
Definition at line 64 of file stoppable_thread_impl.cpp.
References request_stop(), and wait_for_stop().
std::string foedus::thread::StoppableThread::to_string | ( | ) | const |
Definition at line 87 of file stoppable_thread_impl.cpp.
void foedus::thread::StoppableThread::wait_for_stop | ( | ) |
Blocks until the thread stops.
If the thread is not running anyways, this does nothing.
Definition at line 78 of file stoppable_thread_impl.cpp.
References is_stopped().
Referenced by stop().
void foedus::thread::StoppableThread::wakeup | ( | ) |
If the thread is still running and also sleeping, requests the thread to immediately wakeup and do its job.
If the thread is not running or not sleeping, has no effect.
Definition at line 59 of file stoppable_thread_impl.cpp.
References foedus::thread::ConditionVariable::notify_one().
|
friend |
Definition at line 93 of file stoppable_thread_impl.cpp.