| 36 | |
| 37 | |
| 38 | void test_thread (void*) |
| 39 | { |
| 40 | // just sleep for a second |
| 41 | dlib::sleep(1000); |
| 42 | |
| 43 | // Now signal that this thread is ending. First we should get a lock on the |
| 44 | // count_mutex so we can safely mess with thread_count. A convenient way to do this |
| 45 | // is to use an auto_mutex object. Its constructor takes a mutex object and locks |
| 46 | // it right away, it then unlocks the mutex when the auto_mutex object is destructed. |
| 47 | // Note that this happens even if an exception is thrown. So it ensures that you |
| 48 | // don't somehow quit your function without unlocking your mutex. |
| 49 | auto_mutex locker(count_mutex); |
| 50 | --thread_count; |
| 51 | // Now we signal this change. This will cause one thread that is currently waiting |
| 52 | // on a call to count_signaler.wait() to unblock. |
| 53 | count_signaler.signal(); |
| 54 | |
| 55 | // At the end of this function locker goes out of scope and gets destructed, thus |
| 56 | // unlocking count_mutex for us. |
| 57 | } |
| 58 | |
| 59 | int main() |
| 60 | { |