threadtest.c (922B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <unistd.h> 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "thread.h" 9 10 enum { 11 THREADS_SPAWN_NR = 64 12 }; 13 14 /* This gets incremented once by each thread */ 15 static int counter; 16 /* Lock that protects `counter' */ 17 static spinlock_t lock; 18 /* Per thread arguments */ 19 static struct thread_args { 20 int id; 21 } thread_args[THREADS_SPAWN_NR]; 22 23 static int 24 foo(void *arg) 25 { 26 struct thread_args *args = arg; 27 28 acquire(&lock); 29 printf("Entered thread with id %d\n", args->id); 30 counter++; 31 release(&lock); 32 33 return 0; 34 } 35 36 int 37 main(void) 38 { 39 int i; 40 struct thread_ctx *tctx; 41 42 setbuf(stdout, NULL); 43 44 tctx = thread_init(NULL, NULL); 45 spinlock_init(&lock); 46 47 for (i = 0; i < THREADS_SPAWN_NR; i++) { 48 thread_args[i].id = i; 49 thread_register(tctx, foo, &thread_args[i]); 50 } 51 52 thread_wait_all_blocking(tctx); 53 54 thread_exit(tctx); 55 56 return EXIT_SUCCESS; 57 }