File: test_pthread_c11_threads.c

package info (click to toggle)
emscripten 2.0.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,440 kB
  • sloc: ansic: 510,324; cpp: 384,763; javascript: 84,341; python: 51,362; sh: 50,019; pascal: 4,159; makefile: 3,409; asm: 2,150; lisp: 1,869; ruby: 488; cs: 142
file content (66 lines) | stat: -rw-r--r-- 1,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <assert.h>
#include <threads.h>

once_flag flag = ONCE_FLAG_INIT;
_Atomic int counter = 0;

void do_once(void) {
  counter++;
  printf("in do_once\n");
}

int thread_main(void* arg) {
  printf("in thread_main %p\n", thrd_current());
  call_once(&flag, do_once);
  printf("done thread_main\n");
  return 42;
}

int run_with_exit(void* arg) {
  thrd_yield();
  thrd_exit(43);
}

int main(int argc, char* argv[]) {
  int result = 0;
  printf("thrd_current: %p\n", thrd_current());

  // Test call_once
  thrd_t t1;
  thrd_t t2;
  thrd_t t3;
  thrd_t t4;

  thrd_create(&t1, thread_main, NULL);
  thrd_create(&t2, thread_main, NULL);
  thrd_create(&t3, thread_main, NULL);
  thrd_create(&t4, thread_main, NULL);

  assert(!thrd_equal(t1, t2));
  assert(thrd_equal(t1, t1));

  thrd_join(t1, &result);
  thrd_join(t2, &result);
  thrd_join(t3, &result);
  thrd_join(t4, &result);
  assert(result == 42);
  assert(counter == 1);

  // Test thrd_exit return value
  thrd_t t5;
  thrd_create(&t5, run_with_exit, NULL);
  thrd_join(t5, &result);
  assert(result == 43);

  // Test thrd_detach
  thrd_t t6;
  thrd_create(&t6, thread_main, NULL);
  thrd_detach(t6);

#ifdef REPORT_RESULT
  REPORT_RESULT(0);
#endif
  printf("done!\n");
  return 0;
}