-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththreads.c
More file actions
44 lines (37 loc) · 828 Bytes
/
threads.c
File metadata and controls
44 lines (37 loc) · 828 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
38
39
40
41
42
43
44
/* This is free and unencumbered software released into the public domain. */
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
char spinners[] = "aaaa";
static void *
looper(void *p)
{
int idx = (intptr_t) p;
while (1)
{
spinners[idx]++;
if (spinners[idx] > 'z') spinners[idx] = 'a';
printf("%s\r", spinners);
fflush(stdout);
usleep(100000);
}
}
int
main(void)
{
int thread_count = 4;
pthread_t threads[thread_count];
for (intptr_t i = 0; i < thread_count; i++)
{
int e = pthread_create(&threads[i], NULL, looper, (void*)i);
assert(!e);
}
for (int i = 0; i < thread_count; i++)
{
int e = pthread_join(threads[i], NULL);
assert(!e);
}
return 0;
}