Fix thread race in director_thread test

This commit is contained in:
William S Fulton 2017-05-12 07:41:40 +01:00
commit e8e56f74ca

View file

@ -17,7 +17,7 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <iostream> #include <swig_examples_lock.h>
class Foo; class Foo;
extern "C" { extern "C" {
@ -28,8 +28,17 @@ extern "C" {
void* working(void* t); void* working(void* t);
pthread_t thread; pthread_t thread;
#endif #endif
static int thread_terminate = 0; static int thread_terminate = 0;
static SwigExamples::CriticalSection critical_section;
int get_thread_terminate() {
SwigExamples::Lock lock(critical_section);
return thread_terminate;
}
void set_thread_terminate(int value) {
SwigExamples::Lock lock(critical_section);
thread_terminate = value;
}
} }
%} %}
@ -55,7 +64,7 @@ extern "C" {
} }
void stop() { void stop() {
thread_terminate = 1; set_thread_terminate(1);
%#ifdef _WIN32 %#ifdef _WIN32
/*TODO(bhy) what to do for win32? */ /*TODO(bhy) what to do for win32? */
%#else %#else
@ -87,7 +96,7 @@ extern "C" {
#endif #endif
{ {
Foo* f = static_cast<Foo*>(t); Foo* f = static_cast<Foo*>(t);
while ( ! thread_terminate ) { while (!get_thread_terminate()) {
MilliSecondSleep(50); MilliSecondSleep(50);
f->do_foo(); f->do_foo();
} }