-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreading_test.c
More file actions
37 lines (34 loc) · 905 Bytes
/
threading_test.c
File metadata and controls
37 lines (34 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define FMT_LOCKED_DEFAULT_PRINTERS
#define FMT_IMPLEMENTATION
#include "fmt.h"
#include <threads.h>
#include <time.h>
static const struct timespec t_sleep = {
.tv_sec = 0,
.tv_nsec = 1000,
};
int worker(void *arg) {
int number = (int)(uintptr_t)arg;
for (int i = 0; i < 10; ++i) {
fmt_println(
"{}: {} {} {} {} {} {} {} {} {}",
number, "The", "quick","brown", "fox",
"jumps", "over", "the", "lazy", "dog"
);
thrd_yield();
thrd_sleep(&t_sleep, NULL);
thrd_yield();
}
return 0;
}
int main(void) {
fmt_init_threading();
thrd_t threads[10];
static const int COUNT = sizeof(threads) / sizeof(*threads);
for (int i = 0; i < COUNT; ++i) {
thrd_create(threads + i, worker, (void *)(uintptr_t)i);
}
for (int i = 0; i < COUNT; ++i) {
thrd_join(threads[i], NULL);
}
}