cgv
Loading...
Searching...
No Matches
mutex_std_thread.h
1#include "mutex.h"
2#include "common_std_thread.h"
3#include <iostream>
4
5namespace cgv {
6 namespace os {
7
8unsigned& ref_debug_lock_counter()
9{
10 static unsigned counter = 0;
11 return counter;
12}
13
14mutex& ref_debug_mutex()
15{
16 static mutex m;
17 return m;
18}
19
22{
23 return ref_debug_lock_counter();
24}
25
28{
29 Mutex*& m_ptr = ((Mutex*&) pmutex);
30 m_ptr = new Mutex();
31}
32
35{
36 Mutex*& m_ptr = ((Mutex*&) pmutex);
37 delete m_ptr;
38}
39
42void mutex::lock()
43{
44 Mutex& m = *((Mutex*&) pmutex);
45 m.lock();
46}
47
49void mutex::unlock()
50{
51 Mutex& m = *((Mutex*&) pmutex);
52 m.unlock();
53}
54
56void mutex::debug_lock(const std::string& info)
57{
58 ref_debug_mutex().lock();
59 std::cout << info << ": wait for lock [" << this << ":" << get_debug_lock_counter() << "]" << std::endl;
60 ++ref_debug_lock_counter();
61 ref_debug_mutex().unlock();
62 lock();
63 ref_debug_mutex().lock();
64 std::cout << info << ": received lock [" << this << "]" << std::endl;
65 ref_debug_mutex().unlock();
66}
68void mutex::debug_unlock(const std::string& info)
69{
70 unlock();
71 ref_debug_mutex().lock();
72 std::cout << info << ": unlock [" << this << "]" << std::endl;
73 ref_debug_mutex().unlock();
74}
75
77bool mutex::try_lock()
78{
79 Mutex& m = *((Mutex*&) pmutex);
80 return m.try_lock();
81}
82
85{
86 Mutex& m = *((Mutex*&) pmutex);
87 Condition*& c_ptr = (Condition*&) pcond;
88 c_ptr = new Condition(m);
89}
90
93{
94 Condition*& c_ptr = (Condition*&) pcond;
95 delete c_ptr;
96}
97
100{
101 Condition& c = *((Condition*&) pcond);
102 c.cv.notify_one();
103}
104
107{
108 lock();
109 send_signal();
110 unlock();
111}
112
115{
116 Condition& c = *((Condition*&) pcond);
117 c.cv.notify_all();
118}
119
122{
123 lock();
125 unlock();
126}
127
128 }
129}
130
the cgv namespace
Definition print.h:11
void send_signal_with_lock()
prefered approach to send the signal and implemented as {lock();send_signal();unlock();}
~condition_mutex()
destruct a mutex
void send_signal()
send the signal to unblock a thread waiting for the condition represented by this condition_mutex
void broadcast_signal_with_lock()
prefered approach to broadcast the signal and implemented as {lock();broadcast_signal();unlock();}
void broadcast_signal()
broadcast signal to unblock several threads waiting for the condition represented by this condition_m...
condition_mutex()
construct a mutex
void unlock()
unlock the mutex
static unsigned get_debug_lock_counter()
return the global locking counter that is used for mutex debugging
~mutex()
destruct a mutex
void debug_unlock(const std::string &info)
same unlock but with printing debug information
bool try_lock()
try to lock the mutex (return false if the mutex is still locked)
mutex()
construct a mutex
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...
void debug_lock(const std::string &info)
same as lock but with printing debug information