GCC Code Coverage Report


Directory: ./
File: demos/cond_var_timeout.cpp
Date: 2022-06-30 06:29:57
Exec Total Coverage
Lines: 0 30 0.0%
Branches: 0 90 0.0%

Line Branch Exec Source
1 #include <iostream>
2 #include <thread>
3 #include "shared_memory/locked_condition_variable.hpp"
4
5 static int thread_callback()
6 {
7 std::cout << "THREAD: create the condition variable" << std::endl;
8 // get a condition variable
9 shared_memory::LockedConditionVariable cond_var("main_memory", "cond_var");
10
11 cond_var.lock_scope();
12 std::cout << "THREAD: wait" << std::endl;
13 cond_var.wait();
14 std::cout << "THREAD: sleep(11)" << std::endl;
15 sleep(11);
16 std::cout << "THREAD: notify all" << std::endl;
17 cond_var.notify_all();
18 return 0;
19 }
20
21 int main()
22 {
23 shared_memory::delete_segment("main_memory");
24 sleep(1);
25 shared_memory::get_segment("main_memory");
26 sleep(1);
27 std::cout << "MAIN: create thread" << std::endl;
28 std::thread my_thread(&thread_callback);
29 std::cout << "MAIN: sleep(1) so THREAD goes to wait" << std::endl;
30 sleep(1);
31 std::cout << "MAIN: create the condition variable" << std::endl;
32 shared_memory::LockedConditionVariable cond_var("main_memory", "cond_var");
33
34 std::cout << "MAIN: notify thread" << std::endl;
35 cond_var.notify_all();
36
37 std::cout << "MAIN: timed wait" << std::endl;
38 if (!cond_var.timed_wait(5))
39 {
40 std::cout << "MAIN: TIMED_OUT!!!" << std::endl;
41 }
42 else
43 {
44 std::cout << "MAIN: Has been notified" << std::endl;
45 }
46 cond_var.notify_all();
47 my_thread.join();
48 }
49