[libcxx] r344820 - Repair thread-unsafe modifications of n_alive in F.pass.cpp

Billy Robert O'Neal III bion at microsoft.com
Fri Oct 19 16:45:45 PDT 2018


Author: bion
Date: Fri Oct 19 16:45:45 2018
New Revision: 344820

URL: http://llvm.org/viewvc/llvm-project?rev=344820&view=rev
Log:
Repair thread-unsafe modifications of n_alive in F.pass.cpp

In this example, the ctor of G runs in the main thread in the expression G(), and also in the copy ctor of G() in the DECAY_COPY inside std::thread. The main thread destroys the G() instance at the semicolon, and the started thread destroys the G() after it returns. Thus there is a race between the threads on the n_alive variable.

The fix is to join with the background thread before attempting to destroy the G in the main thread.

Modified:
    libcxx/trunk/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp

Modified: libcxx/trunk/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp?rev=344820&r1=344819&r2=344820&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp Fri Oct 19 16:45:45 2018
@@ -157,8 +157,11 @@ int main()
     {
         assert(G::n_alive == 0);
         assert(!G::op_run);
-        std::thread t((G()));
-        t.join();
+        {
+            G g;
+            std::thread t(g);
+            t.join();
+        }
         assert(G::n_alive == 0);
         assert(G::op_run);
     }
@@ -185,8 +188,11 @@ int main()
     {
         assert(G::n_alive == 0);
         assert(!G::op_run);
-        std::thread t(G(), 5, 5.5);
-        t.join();
+        {
+            G g;
+            std::thread t(g, 5, 5.5);
+            t.join();
+        }
         assert(G::n_alive == 0);
         assert(G::op_run);
     }




More information about the libcxx-commits mailing list