File: test_pthread_weak_ref.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (44 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (2)
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
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

bool running = false;

void *worker_thread(void *arg) {
  printf("worker_thread\n");

  pthread_mutex_lock(&mutex);
  running = true;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mutex);

  // Infinite loop
  while (1) {}

  return NULL;
}

int main() {
  pthread_t thread;

  printf("main\n");
  int rc = pthread_create(&thread, NULL, worker_thread, NULL);
  assert(rc == 0);

  pthread_mutex_lock(&mutex);

  // Wait until the thread executes its entry point
  while (!running) {
    pthread_cond_wait(&cond, &mutex);
  }

  pthread_mutex_unlock(&mutex);

  printf("done\n");
  return 0;
}