This test routine is only called by the master thread (thread_num = 0).
| 90 | |
| 91 | // This test routine is only called by the master thread (thread_num = 0). |
| 92 | bool team_example(void) |
| 93 | { bool ok = true; |
| 94 | |
| 95 | size_t num_threads = NUMBER_THREADS; |
| 96 | |
| 97 | // Check that no memory is in use or available at start |
| 98 | // (using thread_alloc in sequential mode) |
| 99 | size_t thread_num; |
| 100 | for(thread_num = 0; thread_num < num_threads; thread_num++) |
| 101 | { ok &= thread_alloc::inuse(thread_num) == 0; |
| 102 | ok &= thread_alloc::available(thread_num) == 0; |
| 103 | } |
| 104 | |
| 105 | // initialize work_all_ |
| 106 | for(thread_num = 0; thread_num < num_threads; thread_num++) |
| 107 | { // allocate separate memory for this thread to avoid false sharing |
| 108 | size_t min_bytes(sizeof(work_one_t)), cap_bytes; |
| 109 | void* v_ptr = thread_alloc::get_memory(min_bytes, cap_bytes); |
| 110 | work_all_[thread_num] = static_cast<work_one_t*>(v_ptr); |
| 111 | // in case this thread's worker does not get called |
| 112 | work_all_[thread_num]->ok = false; |
| 113 | // parameter that defines the work for this thread |
| 114 | work_all_[thread_num]->x = double(thread_num) + 1.; |
| 115 | } |
| 116 | |
| 117 | ok &= team_create(num_threads); |
| 118 | ok &= team_work(worker); |
| 119 | ok &= team_destroy(); |
| 120 | |
| 121 | // go down so that free memrory for other threads before memory for master |
| 122 | thread_num = num_threads; |
| 123 | while(thread_num--) |
| 124 | { // check that this thread was ok with the work it did |
| 125 | ok &= work_all_[thread_num]->ok; |
| 126 | // delete problem specific information |
| 127 | void* v_ptr = static_cast<void*>( work_all_[thread_num] ); |
| 128 | thread_alloc::return_memory( v_ptr ); |
| 129 | // check that there is no longer any memory inuse by this thread |
| 130 | // (for general applications, the master might still be using memory) |
| 131 | ok &= thread_alloc::inuse(thread_num) == 0; |
| 132 | // return all memory being held for future use by this thread |
| 133 | thread_alloc::free_available(thread_num); |
| 134 | } |
| 135 | return ok; |
| 136 | } |
| 137 | // END C++ |