-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_lock_sleep.c
64 lines (57 loc) · 1.28 KB
/
pthread_lock_sleep.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // compile with cc mutex.c -lpthread
int count = 0;
int current_value;
float ftime = 0.6;
pthread_mutex_t pthread_lock = PTHREAD_MUTEX_INITIALIZER;
int inc()
{
if (pthread_mutex_lock(&pthread_lock))
printf("ERROR\n");
current_value = count;
current_value++;
sleep(ftime);
count = current_value;
pthread_mutex_unlock(&pthread_lock);
}
int dec()
{
if (pthread_mutex_lock(&pthread_lock))
printf("ERROR\n");
current_value = count;
sleep(2*ftime);
}
int get_count(int *countp)
{
if (pthread_mutex_lock(&pthread_lock))
printf("ERROR\n");
*countp = count;
pthread_mutex_unlock(&pthread_lock);
}
void *myThreadFun()
{
inc();
dec();
get_count(&count);
printf("%d ", count);
}
int main(int argc, char* argv[])
{
if (argc > 1) {
sscanf(argv[1], "%f", &ftime);
}
ftime = ftime / 6;
pthread_t thread_id[2];
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
for (int i = 0; i < 2; i++)
pthread_create(&(thread_id[i]), NULL, myThreadFun, NULL);
for (int i = 0; i < 2; i++)
pthread_join(thread_id[i], NULL);
printf("\n");
}