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