| 57 | } |
| 58 | |
| 59 | int main() |
| 60 | { |
| 61 | |
| 62 | cout << "Create some threads" << endl; |
| 63 | for (int i = 0; i < thread_count; ++i) |
| 64 | { |
| 65 | // Create some threads. This 0 we are passing in here is the argument that gets |
| 66 | // passed to the thread function (a void pointer) but we aren't using it in this |
| 67 | // example program so i'm just using 0. |
| 68 | create_new_thread(test_thread,0); |
| 69 | } |
| 70 | cout << "Done creating threads, now we wait for them to end" << endl; |
| 71 | |
| 72 | |
| 73 | // Again we use an auto_mutex to get a lock. We don't have to do it this way |
| 74 | // but it is convenient. Also note that we can name the auto_mutex object anything. |
| 75 | auto_mutex some_random_unused_name(count_mutex); |
| 76 | |
| 77 | // Now we wait in a loop for thread_count to be 0. Note that it is important to do this in a |
| 78 | // loop because it is possible to get spurious wakeups from calls to wait() on some |
| 79 | // platforms. So this guards against that and it also makes the code easy to understand. |
| 80 | while (thread_count > 0) |
| 81 | count_signaler.wait(); // This puts this thread to sleep until we get a signal to look at the |
| 82 | // thread_count variable. It also unlocks the count_mutex before it |
| 83 | // goes to sleep and then relocks it when it wakes back up. Again, |
| 84 | // note that it is possible for wait() to return even if no one signals you. |
| 85 | // This is just weird junk you have to deal with on some platforms. So |
| 86 | // don't try to be clever and write code that depends on the number of |
| 87 | // times wait() returns because it won't always work. |
| 88 | |
| 89 | |
| 90 | cout << "All threads done, ending program" << endl; |
| 91 | } |
| 92 | |
| 93 |
nothing calls this directly
no test coverage detected