Last active
April 5, 2021 14:38
-
-
Save ccckmit/9ef8df216239246a9dae13a6d3bb5c52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pthread.h> | |
#define LOOPS 100000 | |
int counter = 0; | |
void *inc() | |
{ | |
for (int i=0; i<LOOPS; i++) { | |
counter = counter + 1; | |
} | |
} | |
void *dec() | |
{ | |
for (int i=0; i<LOOPS; i++) { | |
counter = counter - 1; | |
} | |
} | |
int main() | |
{ | |
pthread_t thread1, thread2; | |
pthread_create(&thread1, NULL, inc, NULL); | |
pthread_create(&thread2, NULL, dec, NULL); | |
pthread_join( thread1, NULL); | |
pthread_join( thread2, NULL); | |
printf("counter=%d\n", counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment