Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <pthread.h>
- void do_one_thing(int *);
- void do_another_thing(int *);
- void do_wrap_up(int);
- int common = 0; /* A shared variable for two threads */
- pthread_mutex_t mut=PTHREAD_MUTEX_INITIALIZER;
- main()
- {
- pthread_t thread1, thread2;
- if (pthread_create(&thread1, NULL, (void *)do_one_thing, (void *)&common)!=0)
- perror("pthread_create"),exit(1);
- if (pthread_create(&thread2,NULL,(void *)do_another_thing,(void *)&common)!=0)
- perror("pthread_create"),exit(1);
- if (pthread_join(thread1, NULL) != 0) perror("pthread_join"), exit(1);
- if (pthread_join(thread2, NULL) != 0) perror("pthread_join"), exit(1);
- do_wrap_up(common);
- return 0;
- }
- void do_one_thing(int *pnum_times)
- {
- int i, j, x;
- unsigned long k;
- int work;
- for (i=0;i<50;i++){
- pthread_mutex_lock(&mut);
- printf("doing one thing\n");
- work = *pnum_times;
- printf("counter = %d\n", work);
- work++; /* increment, but not write */
- for (k=0;k<500000;k++); /* long cycle */
- *pnum_times = work; /* write back */
- pthread_mutex_unlock(&mut);
- }
- }
- void do_another_thing(int *pnum_times)
- {
- int i, j, x;
- unsigned long k;
- int work;
- for (i=0;i<50;i++) {
- pthread_mutex_lock(&mut);
- printf("doing another thing\n");
- work = *pnum_times;
- printf("counter = %d\n", work);
- work++; /* increment, but not write */
- for (k=0;k<500000;k++); /* long cycle */
- *pnum_times = work; /* write back */
- pthread_mutex_unlock(&mut);
- }
- }
- void do_wrap_up(int counter)
- {
- int total;
- printf("All done, counter = %d\n", counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement