ftmemsim-valgrind/drd/tests/local_static.cpp
Bart Van Assche 3dac2e0ca4 drd/tests/local_static: Fix a typo
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14011
2014-06-09 07:38:32 +00:00

54 lines
1010 B
C++

// A C++ compiler is supposed to have thread-safe statics
#include <cstdio>
#include <vector>
#include <pthread.h>
class Singleton
{
public:
Singleton()
: value(42)
{ }
int value;
};
void* thread_func(void*)
{
static Singleton singleton;
fprintf(stderr, "%d\n", singleton.value);
fprintf(stderr, "%d\n", singleton.value);
fprintf(stderr, "%d\n", singleton.value);
fprintf(stderr, "%d\n", singleton.value);
fprintf(stderr, "%d\n", singleton.value);
return 0;
}
int main(int, char**)
{
std::vector<pthread_t> thread(2);
void* v;
for (std::vector<pthread_t>::iterator p = thread.begin(); p != thread.end();
p++) {
if (pthread_create(&*p, 0, thread_func, 0) != 0) {
fprintf(stderr, "Creation of thread %ld failed\n",
&*p - &*thread.begin());
return 1;
}
}
for (std::vector<pthread_t>::const_iterator p = thread.begin();
p != thread.end(); p++) {
pthread_join(*p, &v);
}
fprintf(stderr, "Done.\n");
return 0;
}