20 #include <glog/logging.h>
30 std::thread &&the_thread,
const std::chrono::microseconds &sleep_interval) {
32 thread_ = std::move(the_thread);
33 sleep_interval_ = sleep_interval;
35 stop_requested_ =
false;
37 LOG(INFO) << name_ <<
" initialized. sleep_interval=" << sleep_interval_.count() <<
" microsec";
41 std::thread &&the_thread,
const std::chrono::microseconds &sleep_interval) {
42 std::stringstream str;
43 str << name_prefix << name_ordinal;
44 initialize(str.str(), std::move(the_thread), sleep_interval);
48 VLOG(1) << name_ <<
" sleeping for " << sleep_interval_.count() <<
" microsec";
49 condition_.
wait_for(sleep_interval_);
50 VLOG(1) << name_ <<
" woke up";
52 LOG(INFO) << name_ <<
" stop requested";
60 VLOG(1) <<
"Waking up " << name_ <<
"...";
70 LOG(INFO) <<
"Requesting to stop " << name_ <<
"...";
71 condition_.
notify_one([
this]{ stop_requested_ =
true; });
72 LOG(INFO) <<
"Requested to stop " << name_;
74 LOG(INFO) <<
"Already requested to stop: " << name_;
80 LOG(INFO) <<
"Stopping " << name_ <<
"...";
82 LOG(INFO) <<
"Successfully Stopped " << name_;
88 std::stringstream stream;
94 o <<
"<StoppableThread>"
95 <<
"<name_>" << v.name_ <<
"</name_>"
96 <<
"<native_thread_id>" << v.thread_.get_id() <<
"</native_thread_id>"
97 <<
"<sleep_interval_>" << v.sleep_interval_.count() <<
"</sleep_interval_>"
98 <<
"<started_>" << v.started_.load() <<
"</started_>"
99 <<
"<stop_requested_>" << v.stop_requested_.load() <<
"</stop_requested_>"
100 <<
"<stopped_>" << v.stopped_.load() <<
"</stopped_>"
101 <<
"</StoppableThread>";
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.
bool wait_for(const std::chrono::duration< REP, PERIOD > &timeout, PREDICATE predicate)
Block until the event happens or the given period elapses.
std::string to_string() const
void stop()
request_stop() plus wait_for_stop().
void wait_for_stop()
Blocks until the thread stops.
bool is_stop_requested() const
returns whether someone has requested to stop this.
void notify_one(SIGNAL_ACTION signal_action)
Notify one waiter that the event has happened.
std::ostream & operator<<(std::ostream &o, const ImpersonateSession &v)
void wakeup()
If the thread is still running and also sleeping, requests the thread to immediately wakeup and do it...
The frequently appearing quartet of std::thread, condition_varible, stop-request flag, and mutex.
bool sleep()
Sleep until the interval elapses or someone requests to stop this thread.