ciabatta/test/test_threads.c

35 lines
701 B
C
Raw Normal View History

#include <stdio.h>
#include <threads.h>
#include <stdatomic.h>
2023-02-18 14:58:56 +00:00
_Thread_local int counter;
tss_t key;
2023-02-18 14:58:56 +00:00
int f(void* thr_data) {
tss_set(key, "Thread 2 finished");
2023-02-15 09:54:58 +00:00
for(int n = 0; n < 5; ++n)
2023-02-18 14:58:56 +00:00
counter++;
puts(tss_get(key));
return 0;
}
int main(void)
{
2023-02-18 14:58:56 +00:00
tss_create(&key, NULL);
2023-02-18 15:06:30 +00:00
thrd_t thread;
int status = thrd_create(&thread, f, NULL);
if(status == thrd_error) {
puts("Failed creating threads");
}
for(int n = 0; n < 10; ++n) {
counter++;
}
2023-02-18 14:58:56 +00:00
tss_set(key, "Thread 1 finished");
2023-02-18 15:06:30 +00:00
int res;
if(thrd_join(thread, &res) == thrd_error) {
puts("Failed waiting on thread");
}
2023-02-18 14:58:56 +00:00
puts(tss_get(key));
tss_delete(key);
}