Use a separate variable for the exit condition

This commit is contained in:
Nils O. Selåsdal
2013-12-24 02:30:22 +01:00
parent 89802286c4
commit 423c21bee2
+13 -1
View File
@@ -8,21 +8,24 @@
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int counter = 0;
static int please_exit = 0;
void wake_cb(struct IOMuxWaker *wk)
{
int local_counter;
int exit_now;
int rc;
rc = pthread_mutex_lock(&lock);
assert(rc == 0);
local_counter = counter;
exit_now = please_exit;
printf("counter is %d\n", local_counter);
rc = pthread_mutex_unlock(&lock);
assert(rc == 0);
if (local_counter == 13) {
if (exit_now) {
rc = uc_iomux_waker_destroy(wk);
assert(rc == 0);
}
@@ -59,6 +62,15 @@ void *waker(void *arg)
rc = uc_iomux_waker_signal(s);
assert(rc == 0);
}
rc = pthread_mutex_lock(&lock);
assert(rc == 0);
please_exit = 1;
//must wake it inside the mutex, else we risk a race
//when the waker is destroyed
rc = uc_iomux_waker_signal(s);
assert(rc == 0);
rc = pthread_mutex_unlock(&lock);
return NULL;
}