1 // A C++ compiler is supposed to have thread-safe statics
2
3 #include <cstdio>
4 #include <vector>
5 #include <pthread.h>
6
7 class Singleton
8 {
9 public:
Singleton()10 Singleton()
11 : value(42)
12 { }
13
14 int value;
15 };
16
thread_func(void *)17 void* thread_func(void*)
18 {
19 static Singleton singleton;
20
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);
26
27 return 0;
28 }
29
main(int,char **)30 int main(int, char**)
31 {
32 std::vector<pthread_t> thread(2);
33 void* v;
34
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;
41 }
42 }
43
44 for (std::vector<pthread_t>::const_iterator p = thread.begin();
45 p != thread.end(); p++) {
46 pthread_join(*p, &v);
47 }
48
49 fprintf(stderr, "Done.\n");
50
51 return 0;
52 }
53
54