2022-06-29 07:21:35 +00:00
|
|
|
#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");
|
|
|
|
}
|
2022-06-29 07:21:35 +00:00
|
|
|
|
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");
|
2022-06-29 07:21:35 +00:00
|
|
|
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
|
|
|
}
|
2022-06-29 07:21:35 +00:00
|
|
|
}
|