Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- int currentBalance=100,newBalance=0,oldBalance=0;
- pthread_mutex_t mutex;
- void* thread1(){
- pthread_mutex_lock(&mutex);
- oldBalance = currentBalance;
- newBalance = oldBalance + 10000;
- currentBalance = newBalance;
- pthread_mutex_unlock(&mutex);
- }
- void* thread2(){
- pthread_mutex_lock(&mutex);
- oldBalance = currentBalance;
- newBalance = oldBalance + 5000;
- currentBalance = newBalance;
- pthread_mutex_unlock(&mutex);
- }
- void* thread3(){
- pthread_mutex_lock(&mutex);
- currentBalance = currentBalance;
- newBalance = oldBalance + 5500;
- currentBalance = currentBalance;
- pthread_mutex_unlock(&mutex);
- }
- int main()
- {
- pthread_t trd1,trd2,trd3;
- pthread_attr_t attr;
- pthread_mutex_init(&mutex,NULL);
- pthread_attr_init(&attr);
- pthread_create(&trd1,NULL,thread1,NULL);
- pthread_join(trd1,NULL);
- pthread_create(&trd2,NULL,thread2,NULL);
- pthread_join(trd2,NULL);
- pthread_create(&trd3,NULL,thread3,NULL);
- pthread_join(trd3,NULL);
- printf("Current Balance: %dn",currentBalance);
- return 0;
- }
- gcc -pthread task6.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement