1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | #include <pthread.h>
|
|---|
| 2 | #include <assert.h>
|
|---|
| 3 |
|
|---|
| 4 | pthread_mutex_t mutex;
|
|---|
| 5 | int data = 0;
|
|---|
| 6 |
|
|---|
| 7 | void *thread1(void *arg)
|
|---|
| 8 | {
|
|---|
| 9 | pthread_mutex_lock(&mutex);
|
|---|
| 10 | data++;
|
|---|
| 11 | pthread_mutex_unlock(&mutex);
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | void *thread2(void *arg)
|
|---|
| 16 | {
|
|---|
| 17 | pthread_mutex_lock(&mutex);
|
|---|
| 18 | data+=2;
|
|---|
| 19 | pthread_mutex_unlock(&mutex);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | void *thread3(void *arg)
|
|---|
| 24 | {
|
|---|
| 25 | pthread_mutex_lock(&mutex);
|
|---|
| 26 | if (data >= 3){
|
|---|
| 27 | ERROR: goto ERROR;
|
|---|
| 28 | }
|
|---|
| 29 | pthread_mutex_unlock(&mutex);
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | int main()
|
|---|
| 34 | {
|
|---|
| 35 | pthread_mutex_init(&mutex, 0);
|
|---|
| 36 |
|
|---|
| 37 | pthread_t t1, t2, t3;
|
|---|
| 38 |
|
|---|
| 39 | pthread_create(&t1, 0, thread1, 0);
|
|---|
| 40 | pthread_create(&t2, 0, thread2, 0);
|
|---|
| 41 | pthread_create(&t3, 0, thread3, 0);
|
|---|
| 42 |
|
|---|
| 43 | pthread_join(t1, 0);
|
|---|
| 44 | pthread_join(t2, 0);
|
|---|
| 45 | pthread_join(t3, 0);
|
|---|
| 46 |
|
|---|
| 47 | return 0;
|
|---|
| 48 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.