Skip to content

Commit 4f5be19

Browse files
committed
Incorporate feedback
1 parent 0f6f592 commit 4f5be19

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16380
1+
16368

tests/pthread/test_pthread_atexit.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
#include <assert.h>
22
#include <pthread.h>
3+
#include <stdbool.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56

6-
pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;
7-
pthread_cond_t count_nonzero = PTHREAD_COND_INITIALIZER;
8-
unsigned count;
7+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
8+
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
9+
bool should_exit;
910

1011
pthread_t thread;
1112

1213
void *workerThread(void* arg) {
13-
pthread_mutex_lock(&count_lock);
14-
while (count == 0)
15-
pthread_cond_wait(&count_nonzero, &count_lock);
16-
count--;
17-
pthread_mutex_unlock(&count_lock);
14+
pthread_mutex_lock(&mutex);
15+
while (!should_exit)
16+
pthread_cond_wait(&cond, &mutex);
17+
pthread_mutex_unlock(&mutex);
1818

1919
return NULL;
2020
}
2121

2222
void terminateThread() {
23-
pthread_mutex_lock(&count_lock);
24-
if (count == 0)
25-
pthread_cond_signal(&count_nonzero);
26-
count++;
27-
pthread_mutex_unlock(&count_lock);
23+
pthread_mutex_lock(&mutex);
24+
should_exit = true;
25+
pthread_cond_signal(&cond);
26+
pthread_mutex_unlock(&mutex);
2827

2928
int res = 0;
3029
int rc = pthread_join(thread, (void**)&res);
3130
assert(rc == 0);
3231
assert(res == 0);
3332

34-
printf("done waiting - counter is: %d\n", count);
33+
printf("done waiting - thread successfully terminated\n");
3534
}
3635

3736
int main(int argc, char* argv[]) {

tests/pthread/test_pthread_atexit.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
done waiting - counter is: 0
1+
done waiting - thread successfully terminated

0 commit comments

Comments
 (0)