-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_ex_3.c
140 lines (101 loc) · 3.21 KB
/
thread_ex_3.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#define NTHREADS 10
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t evens_done = PTHREAD_COND_INITIALIZER;
int number_evens_finished = 0;
int counter = 0;
void* func1(void* data)
{
int *x = (int *) data;
pthread_mutex_lock( &lock);
if (*x % 2 == 0) {
number_evens_finished++;
}
else {
pthread_cond_wait(&evens_done, &lock);
}
counter++;
printf("message is %d, thread id = %lud modified the counter to %d\n", *x, pthread_self(), counter);
printf("message is %d, thread id = %lud read the counter %d\n", *x, pthread_self(), counter);
pthread_mutex_unlock( &lock );
}
int main(void)
{
pthread_t thread_id[NTHREADS];
int values[NTHREADS];
for(int i=0; i < NTHREADS; i++) {
values[i] = i;
pthread_create( &thread_id[i], NULL, func1, &values[i]);
}
// NEED TO WAIT UNTIL ALL THREADS HAVE BEEN CREATED AND RUNNING (SO THAT ALL ODD THREADS ARE WAITING, BEFORE WE
// EVEN TRY TO SIGNAL (OR THERE WILL BE A RACE CONDITION, RAN INTO THE PROBLEM OF SIGNALLING BEFORE ALL ODD THREADS WERE WAITING
sleep(1);
while(1) {
if(number_evens_finished == NTHREADS / 2) {
pthread_cond_broadcast(&evens_done);
break;
}
}
for(int j=0; j < NTHREADS; j++) {
pthread_join( thread_id[j], NULL);
pthread_exit(&thread_id[j]);
}
return 0;
}
/* SOLUTION USING TWO LOCKS AND RUNNING 1000 times */
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#define NTHREADS 10000
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutext = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t evens_done = PTHREAD_COND_INITIALIZER;
int number_evens_finished = 0;
int counter = 0;
void* func1(void* data)
{
int *x = (int *) data;
pthread_mutex_lock( &condition_mutext );
if (*x % 2 == 0) {
number_evens_finished++;
}
else {
pthread_cond_wait(&evens_done, &condition_mutext);
}
pthread_mutex_unlock( &condition_mutext );
pthread_mutex_lock( &lock );
counter++;
printf("message is %d, thread id = %lud modified the counter to %d\n", *x, pthread_self(), counter);
printf("message is %d, thread id = %lud read the counter %d\n", *x, pthread_self(), counter);
pthread_mutex_unlock( &lock );
}
int main(void)
{
pthread_t thread_id[NTHREADS];
int values[NTHREADS];
for (int k = 0; k < 1000; k++) {
for(int i=0; i < NTHREADS; i++) {
values[i] = i;
pthread_create( &thread_id[i], NULL, func1, &values[i]);
}
// NEED TO WAIT UNTIL ALL THREADS HAVE BEEN CREATED AND RUNNING (SO THAT ALL ODD THREADS ARE WAITING, BEFORE WE
// EVEN TRY TO SIGNAL (OR THERE WILL BE A RACE CONDITION, RAN INTO THE PROBLEM OF SIGNALLING BEFORE ALL ODD THREADS WERE WAITING
sleep(1);
while(1) {
if(number_evens_finished == NTHREADS / 2) {
pthread_cond_broadcast(&evens_done);
break;
}
}
for(int j=0; j < NTHREADS; j++) {
pthread_join( thread_id[j], NULL);
}
number_evens_finished = 0;
}
for(int j=0; j < NTHREADS; j++) {
pthread_exit(&thread_id[j]);
}
return 0;
}