|
|
@@ -3,16 +3,21 @@
|
|
3
|
3
|
#include "mythreads.h"
|
|
4
|
4
|
|
|
5
|
5
|
int balance = 0;
|
|
|
6
|
+pthread_mutex_t mut;
|
|
6
|
7
|
|
|
7
|
8
|
void* worker(void* arg) {
|
|
|
9
|
+ pthread_mutex_lock(&mut);
|
|
8
|
10
|
balance++; // unprotected access
|
|
|
11
|
+ pthread_mutex_unlock(&mut);
|
|
9
|
12
|
return NULL;
|
|
10
|
13
|
}
|
|
11
|
14
|
|
|
12
|
15
|
int main(int argc, char *argv[]) {
|
|
13
|
16
|
pthread_t p;
|
|
14
|
17
|
Pthread_create(&p, NULL, worker, NULL);
|
|
|
18
|
+ pthread_mutex_lock(&mut);
|
|
15
|
19
|
balance++; // unprotected access
|
|
|
20
|
+ pthread_mutex_unlock(&mut);
|
|
16
|
21
|
Pthread_join(p, NULL);
|
|
17
|
22
|
return 0;
|
|
18
|
23
|
}
|