drd: Add a consistency check
[valgrind.git] / drd / tests / std_atomic.cpp
blob9cdc1e56e10d6e3ee9764349d7f3c4c6d6b296a1
1 /*
2 * Test program for std::atomic<>
4 * See also https://bugs.kde.org/show_bug.cgi?id=328490.
5 */
7 #include "../drd.h"
8 #include <atomic>
9 #include <iostream>
10 #include <string>
11 #include <pthread.h>
13 std::atomic<bool> g_b;
15 void *func1(void *instance)
17 while (!g_b) {
18 timespec delay = { 0, 100 * 1000 * 1000 };
19 nanosleep(&delay, NULL);
21 return NULL;
24 void *func2(void *instance)
26 g_b = true;
27 return NULL;
30 int main(int argc, char* argv[])
32 int err;
33 pthread_t thread1;
34 pthread_t thread2;
36 std::cerr << "Started.\n";
38 if (argc > 1)
39 DRD_IGNORE_VAR(g_b);
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.");
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.");
55 std::cerr << "Done.\n";