Skip to content

Instantly share code, notes, and snippets.

@ccckmit
Last active April 5, 2021 14:38
Show Gist options
  • Save ccckmit/9ef8df216239246a9dae13a6d3bb5c52 to your computer and use it in GitHub Desktop.
Save ccckmit/9ef8df216239246a9dae13a6d3bb5c52 to your computer and use it in GitHub Desktop.
#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