Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<pthread.h>
- int global_variable;
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- void * change_value(void * arg)
- {
- int operation = (int) arg, i;
- for (i = 0; i < 100000; i++)
- {
- pthread_mutex_lock(&mutex);
- if (operation == 0)
- {
- global_variable++;
- }
- else
- {
- global_variable--;
- }
- pthread_mutex_unlock(&mutex);
- printf("%d ", global_variable);
- fflush(stdout);
- }
- }
- int main()
- {
- printf("Hello \n");
- pthread_t addition, substraction;
- pthread_create(&addition, NULL, change_value, (void *) 0);
- pthread_create(&substraction, NULL, change_value, (void*) 1);
- pthread_join(addition, NULL);
- pthread_join(substraction, NULL);
- pthread_exit(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement