-
Notifications
You must be signed in to change notification settings - Fork 5
/
threads.c
46 lines (40 loc) · 992 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
double get_time() {
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double) t.tv_sec + (double) t.tv_usec / 1e6;
}
void spin(int howlong) {
double t = get_time();
while ((get_time() - t) < (double) howlong) ;
}
int counter = 0;
typedef struct {
pthread_t thread;
int id;
} ThreadInfo;
void *worker(void *arg) {
ThreadInfo* threadInfo = (ThreadInfo*) arg;
while (counter < 10) {
spin(1);
counter++;
printf("[Thread %d] counter value : %d\n", threadInfo->id, counter);
}
return NULL;
}
int main() {
ThreadInfo thread1 = { .id = 1 };
ThreadInfo thread2 = { .id = 2 };
pthread_create(&thread1.thread, NULL, worker, &thread1);
pthread_create(&thread2.thread, NULL, worker, &thread2);
pthread_join(thread1.thread, NULL);
pthread_join(thread2.thread, NULL);
return 0;
}