diff options
Diffstat (limited to 'targets/threads.c')
| -rw-r--r-- | targets/threads.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/targets/threads.c b/targets/threads.c new file mode 100644 index 0000000..ab4d80d --- /dev/null +++ b/targets/threads.c @@ -0,0 +1,26 @@ +// Two threads and a mutex: each adds its share to `total`, main joins them +// and prints it. What clone, futex and a thread's exit look like to a tracer. +#include <pthread.h> +#include <stdio.h> + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +long total; + +__attribute__((noinline)) static void *work(void *arg) { + for (long i = 0; i < 1000; i++) { + pthread_mutex_lock(&lock); + total += (long)arg; + pthread_mutex_unlock(&lock); + } + return NULL; +} + +int main(void) { + pthread_t a, b; + pthread_create(&a, NULL, work, (void *)1); + pthread_create(&b, NULL, work, (void *)2); + pthread_join(a, NULL); + pthread_join(b, NULL); + printf("total = %ld\n", total); + return 0; +} |
