File tree Expand file tree Collapse file tree 2 files changed +16
-13
lines changed Expand file tree Collapse file tree 2 files changed +16
-13
lines changed Original file line number Diff line number Diff line change 3
3
#include <stdio.h>
4
4
#include <stdlib.h>
5
5
6
- #include "emscripten.h"
7
- #include "emscripten/threading.h"
6
+ pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER ;
7
+ pthread_cond_t count_nonzero = PTHREAD_COND_INITIALIZER ;
8
+ unsigned count ;
8
9
9
- static _Atomic int threadCounter = 0 ;
10
- static _Atomic int running = 1 ;
11
- static pthread_t thread ;
10
+ pthread_t thread ;
12
11
13
12
void * workerThread (void * arg ) {
14
- threadCounter ++ ;
15
-
16
- while (running )
17
- emscripten_thread_sleep (1000 );
18
-
19
- threadCounter -- ;
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 );
20
18
21
19
return NULL ;
22
20
}
23
21
24
22
void terminateThread () {
25
- running = 0 ;
23
+ pthread_mutex_lock (& count_lock );
24
+ if (count == 0 )
25
+ pthread_cond_signal (& count_nonzero );
26
+ count ++ ;
27
+ pthread_mutex_unlock (& count_lock );
26
28
27
29
int res = 0 ;
28
30
int rc = pthread_join (thread , (void * * )& res );
29
31
assert (rc == 0 );
30
32
assert (res == 0 );
31
33
32
- printf ("done waiting - counter is: %d\n" , threadCounter );
34
+ printf ("done waiting - counter is: %d\n" , count );
33
35
}
34
36
35
37
int main (int argc , char * argv []) {
Original file line number Diff line number Diff line change @@ -2324,6 +2324,7 @@ def test_pthread_dispatch_after_exit(self):
2324
2324
2325
2325
@node_pthreads
2326
2326
def test_pthread_atexit (self ):
2327
+ # Test whether we can terminate a running thread during atexit.
2327
2328
self .set_setting ('EXIT_RUNTIME' )
2328
2329
self .set_setting ('PTHREAD_POOL_SIZE' , 1 )
2329
2330
self .do_run_in_out_file_test ('pthread/test_pthread_atexit.c' )
You can’t perform that action at this time.
0 commit comments