线程同步
7 #include<errno.h>
8 #include<pthread.h>
9 int counter = 0;
10 void *pd(void* arg)
11 {
12 int val,i;
13 for(i = 0;i < N ;i++)
14 {
15 val = counter;
16 printf("%x:%d\n",(unsigned int)pthread_self(),val+1 );
17 counter = val + 1;
18 usleep(10);
19 }
20
21 return NULL;
22 }
23 int main(int argc,char* argv[])
24 {
25 int a,b;
26 pthread_t ptid1,ptid2;
27 a = pthread_create(&ptid1,NULL,pd,NULL);
28 b = pthread_create(&ptid2,NULL,pd,NULL);
29 if(a != 0)
30 {
31 fprintf(stderr,"cannt accees%s\n",strerror(errno));
32 exit(1);
33 }
34 if(b != 0)
35 {
36 fprintf(stderr,"cannt accees%s\n",strerror(errno));
37 exit(1);
38 }
39
40 pthread_join(ptid1,NULL);
41 pthread_join(ptid2,NULL);
42 return 0;
43 }
7 #include<errno.h>
8 #include<pthread.h>
9 int counter = 0;
10 void *pd(void* arg)
11 {
12 int val,i;
13 for(i = 0;i < N ;i++)
14 {
15 val = counter;
16 printf("%x:%d\n",(unsigned int)pthread_self(),val+1 );
17 counter = val + 1;
18 usleep(10);
19 }
20
21 return NULL;
22 }
23 int main(int argc,char* argv[])
24 {
25 int a,b;
26 pthread_t ptid1,ptid2;
27 a = pthread_create(&ptid1,NULL,pd,NULL);
28 b = pthread_create(&ptid2,NULL,pd,NULL);
29 if(a != 0)
30 {
31 fprintf(stderr,"cannt accees%s\n",strerror(errno));
32 exit(1);
33 }
34 if(b != 0)
35 {
36 fprintf(stderr,"cannt accees%s\n",strerror(errno));
37 exit(1);
38 }
39
40 pthread_join(ptid1,NULL);
41 pthread_join(ptid2,NULL);
42 return 0;
43 }