drd/tests/swapcontext: Improve the portability of this test further
[valgrind.git] / drd / tests / local_static.cpp
blobe84a3158b242fbfc667342ba0e1410009e76a587
1 // A C++ compiler is supposed to have thread-safe statics
3 #include <cstdio>
4 #include <vector>
5 #include <pthread.h>
7 class Singleton
9 public:
10 Singleton()
11 : value(42)
12 { }
14 int value;
17 void* thread_func(void*)
19 static Singleton singleton;
21 fprintf(stderr, "%d\n", singleton.value);
22 fprintf(stderr, "%d\n", singleton.value);
23 fprintf(stderr, "%d\n", singleton.value);
24 fprintf(stderr, "%d\n", singleton.value);
25 fprintf(stderr, "%d\n", singleton.value);
27 return 0;
30 int main(int, char**)
32 std::vector<pthread_t> thread(2);
33 void* v;
35 for (std::vector<pthread_t>::iterator p = thread.begin(); p != thread.end();
36 p++) {
37 if (pthread_create(&*p, 0, thread_func, 0) != 0) {
38 fprintf(stderr, "Creation of thread %d failed\n",
39 (int)(&*p - &*thread.begin()));
40 return 1;
44 for (std::vector<pthread_t>::const_iterator p = thread.begin();
45 p != thread.end(); p++) {
46 pthread_join(*p, &v);
49 fprintf(stderr, "Done.\n");
51 return 0;