File tree Expand file tree Collapse file tree 1 file changed +26
-37
lines changed
libcxx/test/std/thread/thread.condition/thread.condition.condvarany Expand file tree Collapse file tree 1 file changed +26
-37
lines changed Original file line number Diff line number Diff line change 17
17
#include < condition_variable>
18
18
#include < mutex>
19
19
#include < thread>
20
+ #include < vector>
20
21
#include < cassert>
21
22
22
23
#include " test_macros.h"
@@ -28,49 +29,37 @@ typedef std::unique_lock<L0> L1;
28
29
29
30
L0 m0;
30
31
31
- int test0 = 0 ;
32
- int test1 = 0 ;
33
- int test2 = 0 ;
32
+ const unsigned threadCount = 2 ;
33
+ bool pleaseExit = false ;
34
+ std::atomic< unsigned > notReady ;
34
35
35
- void f1 ()
36
- {
37
- L1 lk (m0);
38
- assert (test1 == 0 );
39
- while (test1 == 0 )
40
- cv.wait (lk);
41
- assert (test1 == 1 );
42
- test1 = 2 ;
43
- }
44
-
45
- void f2 ()
46
- {
47
- L1 lk (m0);
48
- assert (test2 == 0 );
49
- while (test2 == 0 )
50
- cv.wait (lk);
51
- assert (test2 == 1 );
52
- test2 = 2 ;
36
+ void helper () {
37
+ L1 lk (m0);
38
+ --notReady;
39
+ while (pleaseExit == false )
40
+ cv.wait (lk);
53
41
}
54
42
55
43
int main (int , char **)
56
44
{
57
- std::thread t1 (f1);
58
- std::thread t2 (f2);
59
- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
60
- {
61
- L1 lk (m0);
62
- test1 = 1 ;
63
- test2 = 1 ;
64
- }
45
+ notReady = threadCount;
46
+ std::vector<std::thread> threads;
47
+ for (unsigned i = 0 ; i < threadCount; i++)
48
+ threads.push_back (std::thread (helper));
49
+ {
50
+ while (notReady > 0 )
51
+ std::this_thread::yield ();
52
+ // At this point, both threads have had a chance to acquire the lock and are
53
+ // either waiting on the condition variable or about to wait.
54
+ L1 lk (m0);
55
+ pleaseExit = true ;
56
+ // POSIX does not guarantee reliable scheduling if notify_all is called
57
+ // without the lock being held.
65
58
cv.notify_all ();
66
- {
67
- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
68
- L1 lk (m0);
69
- }
70
- t1.join ();
71
- t2.join ();
72
- assert (test1 == 2 );
73
- assert (test2 == 2 );
59
+ }
60
+ // The test will hang if not all of the threads were woken.
61
+ for (unsigned i = 0 ; i < threadCount; i++)
62
+ threads[i].join ();
74
63
75
64
return 0 ;
76
65
}
You can’t perform that action at this time.
0 commit comments