-
Notifications
You must be signed in to change notification settings - Fork 9
/
problem.cc
69 lines (54 loc) · 1.55 KB
/
problem.cc
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
// Taken from Unix Network Programming: The Sockets Networking API, Volume 1,
// Third Edition, by W. Richard Stevens, Bill Fenner, and Andrew M. Rudoff
// Page 699
// This program demonstrates a synchronization problem. Two threads
// share a counter variable that is initialized to zero. They each
// try to increment the counter 100 times each. The result of the
// program should be that the counter ends up at 200. However, if
// a synchronization problem exists, the counter will end up at a different
// value.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
struct count {
int counter;
};
void *doit(void *);
int
main(int argc, char **argv)
{
pthread_t tidA, tidB;
// counter in shared memory
struct count c;
c.counter = 0;
srandom(1000);
// create two threads
pthread_create(&tidA, NULL, &doit, &c);
pthread_create(&tidB, NULL, &doit, &c);
// wait for both threads to terminate
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
exit(0);
}
void *
doit(void *vptr)
{
int i, val;
long r;
struct count* c;
c = (struct count*) vptr;
// Each thread fetches, prints, and increments the counter 100 times.
// We use sleep to represent work being done in the meantime.
for (i = 0; i < 100; i++) {
val = c->counter;
r = random() % 100;
usleep(r);
cout << pthread_self() << " " << val + 1 << endl;
c->counter = val + 1;
r = random() % 100;
usleep(r);
}
}