Fix stop_ thread race
authorDave Watson <davejwatson@fb.com>
Fri, 26 Jun 2015 16:19:32 +0000 (09:19 -0700)
committerSara Golemon <sgolemon@fb.com>
Fri, 26 Jun 2015 18:45:40 +0000 (11:45 -0700)
Summary: make it std::atomic

Reviewed By: @tudor

Differential Revision: D2191861

folly/io/async/EventBase.cpp
folly/io/async/EventBase.h

index b8a1c25675b6c9c408f7fc1260d3bbf0621916be..9280f001513551a65a0929584129675b7a22c710 100644 (file)
@@ -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
index 369754bd8535e38b589a01b937a482f79467ff08..872d7ada069ec05232de13acce14d6590a933c54 100644 (file)
@@ -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<bool> stop_;
 
   // The ID of the thread running the main loop.
   // 0 if loop is not running.