bart | ade6988 | 2013-12-10 17:56:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test program for std::atomic<> |
| 3 | * |
| 4 | * See also https://bugs.kde.org/show_bug.cgi?id=328490. |
| 5 | */ |
| 6 | |
| 7 | #include "../drd.h" |
| 8 | #include <atomic> |
| 9 | #include <iostream> |
| 10 | #include <string> |
| 11 | #include <pthread.h> |
| 12 | |
| 13 | std::atomic<bool> g_b; |
| 14 | |
| 15 | void *func1(void *instance) |
| 16 | { |
| 17 | while (!g_b) { |
| 18 | timespec delay = { 0, 100 * 1000 * 1000 }; |
| 19 | nanosleep(&delay, NULL); |
| 20 | } |
| 21 | return NULL; |
| 22 | } |
| 23 | |
| 24 | void *func2(void *instance) |
| 25 | { |
| 26 | g_b = true; |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | int main(int argc, char* argv[]) |
| 31 | { |
| 32 | int err; |
| 33 | pthread_t thread1; |
| 34 | pthread_t thread2; |
| 35 | |
| 36 | std::cerr << "Started.\n"; |
| 37 | |
| 38 | if (argc > 1) |
| 39 | DRD_IGNORE_VAR(g_b); |
| 40 | |
| 41 | err = pthread_create(&thread1, NULL, &func1, NULL); |
| 42 | if (err != 0) |
| 43 | throw std::string("failed to create a thread."); |
| 44 | err = pthread_create(&thread2, NULL, &func2, NULL); |
| 45 | if (err != 0) |
| 46 | throw std::string("failed to create a thread."); |
| 47 | |
| 48 | err = pthread_join(thread1, NULL); |
| 49 | if (err != 0) |
| 50 | throw std::string("Thread::join(): failed to join."); |
| 51 | err = pthread_join(thread2, NULL); |
| 52 | if (err != 0) |
| 53 | throw std::string("Thread::join(): failed to join."); |
| 54 | |
| 55 | std::cerr << "Done.\n"; |
| 56 | } |