Skip to content

Commit a60eacc

Browse files
refs #154 @1h
We now need to imagine a way to tests notifications.
1 parent aea9328 commit a60eacc

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

include/pthread/condition_variable.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ namespace pthread {
238238
int rc = 0;
239239
cv_status status = no_timeout;
240240

241-
milliseconds(millis); // update timeou
241+
milliseconds(millis); // update timeout
242242
bool stop_waiting = lambda(); // returns ​false if the waiting should be continued.
243243

244244
while((! stop_waiting) && (status == no_timeout)){

src/condition_variable.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ namespace pthread {
9191
}
9292

9393
condition_variable::~condition_variable () {
94+
pthread_cond_destroy(&_condition);
95+
/* NOSONAR
9496
int rc = pthread_cond_destroy(&_condition);
95-
// if (rc != 0){
96-
// throw condition_variable_exception("pthread condition variable destroy failed.", rc);
97-
// }
97+
if (rc != 0){
98+
throw condition_variable_exception("pthread condition variable destroy failed.", rc);
99+
}
100+
*/
98101
}
99102

100103
} // namespace pthread

tests/concurrency_tests.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,41 @@ TEST(concurrency, read_write_lock) {
5757
TEST(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

Comments
 (0)