-
Notifications
You must be signed in to change notification settings - Fork 160
/
threads.c
47 lines (44 loc) · 942 Bytes
/
threads.c
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
45
46
47
#define _GNU_SOURCE
#include <assert.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void* thread_0(void *arg) {
int i;
i = 0;
while (true) {
printf("t0 %d\n", i);
sleep(1);
i++;
}
return NULL;
}
void* thread_1(void *arg) {
int i;
i = 0;
while (true) {
printf("t1 %d\n", i);
sleep(1);
i++;
}
return NULL;
}
int main(void) {
enum NUM_THREADS {NUM_THREADS = 2};
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
int i;
pthread_create(&threads[0], NULL, thread_0, (void*)&thread_args[0]);
pthread_create(&threads[1], NULL, thread_1, (void*)&thread_args[1]);
pthread_setname_np(threads[1], "myname1");
while (true) {
printf("main %d\n", i);
sleep(1);
i++;
}
return EXIT_SUCCESS;
}