@@ -57,16 +57,41 @@ TEST(concurrency, read_write_lock) {
5757TEST (concurrency, condition_variable_wait_for){
5858 pthread::condition_variable condition;
5959 pthread::mutex mutex;
60+ bool stop_waiting = true ;
6061
62+ /* wait 1s for condition to be signaled. When returning from the wait_for method call, the mutex is locked.
63+ *
64+ * Therefore we expect, try_lock to throw an exception to signal that the mutex is already locked.
65+ */
6166 EXPECT_EQ (pthread::cv_status::timedout, condition.wait_for (mutex, 1 *1000 ));
6267 EXPECT_THROW (mutex.try_lock (), pthread::pthread_exception);
63- mutex.unlock ();
68+ mutex.unlock (); // free to lock
6469
6570 {
6671 pthread::lock_guard<pthread::mutex> lock{mutex};
6772 EXPECT_EQ (pthread::cv_status::timedout, condition.wait_for (lock, 1 * 1000 ));
6873 }
69- EXPECT_NO_THROW (mutex.try_lock ());
74+
75+ {
76+ pthread::lock_guard<pthread::mutex> lock{mutex};
77+ EXPECT_EQ (true , condition.wait_for (lock, 1 * 1000 , [stop_waiting]{
78+ std::cout << " running lambda, stop_waiting : " << stop_waiting << std::endl ;
79+ return stop_waiting;
80+ }
81+ )
82+ );
83+ }
84+
85+
86+ {
87+ pthread::lock_guard<pthread::mutex> lock{mutex};
88+ EXPECT_EQ (false , condition.wait_for (lock, 1 * 1000 , [stop_waiting]{
89+ std::cout << " running lambda, stop_waiting : " << stop_waiting << std::endl ;
90+ return ! stop_waiting;
91+ }
92+ )
93+ );
94+ }
7095}
7196
7297/* NOSONAR for later use
0 commit comments