From: Dave Watson Date: Fri, 26 Jun 2015 16:19:32 +0000 (-0700) Subject: Fix stop_ thread race X-Git-Tag: v0.48.0~6 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a2206d8115d4ae508a45c5960425e858808ea869;p=folly.git Fix stop_ thread race Summary: make it std::atomic Reviewed By: @tudor Differential Revision: D2191861 --- diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index b8a1c256..9280f001 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -315,8 +315,7 @@ bool EventBase::loopBody(int flags) { std::chrono::steady_clock::now().time_since_epoch()).count(); } - // TODO: Read stop_ atomically with an acquire barrier. - while (!stop_) { + while (!stop_.load(std::memory_order_acquire)) { ++nextLoopCnt_; // Run the before loop callbacks diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index 369754bd..872d7ada 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -671,7 +671,7 @@ bool runImmediatelyOrRunInEventBaseThreadAndWait(const Cob& fn); // stop_ is set by terminateLoopSoon() and is used by the main loop // to determine if it should exit - bool stop_; + std::atomic stop_; // The ID of the thread running the main loop. // 0 if loop is not running.