ciabatta/test/test_threads.c

38 lines
752 B
C
Raw Normal View History

#include <stdio.h>
#include <threads.h>
#include <stdatomic.h>
2023-02-18 23:34:21 +00:00
#define N_THREADS 5
2023-02-18 23:27:59 +00:00
2023-02-18 14:58:56 +00:00
_Thread_local int counter;
2023-02-18 23:27:59 +00:00
once_flag flag = ONCE_FLAG_INIT;
void init() {
puts("Hey I got a call");
}
2023-02-18 14:58:56 +00:00
int f(void* thr_data) {
2023-02-18 23:27:59 +00:00
call_once(&flag, init);
2023-02-15 09:54:58 +00:00
for(int n = 0; n < 5; ++n)
2023-02-18 14:58:56 +00:00
counter++;
2023-02-18 23:27:59 +00:00
puts("Finished");
return 0;
}
int main(void)
{
2023-02-18 23:27:59 +00:00
thrd_t thread[N_THREADS];
for(int i = 0; i != N_THREADS; ++i) {
int status = thrd_create(&thread[i], f, NULL);
if(status == thrd_error) {
puts("Failed creating threads");
}
2023-02-18 15:06:30 +00:00
}
2023-02-18 23:27:59 +00:00
for(int i = 0; i != N_THREADS; ++i) {
int res;
if(thrd_join(thread[i], &res) == thrd_error) {
puts("Failed waiting on thread");
}
2023-02-18 15:06:30 +00:00
}
}