Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created April 19, 2012 23:19
Show Gist options
  • Save dcolish/2424818 to your computer and use it in GitHub Desktop.
Save dcolish/2424818 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#define atomic_int_add(ptr, val) __sync_add_and_fetch(&ptr, val)
int counter = 0;
void* worker(void* val){
int i;
#ifdef ATOMIC
for (i = 0; i < 500; i++) { printf("%d\n", atomic_int_add(counter, 1));}
#else
for (i = 0; i < 500; i++) { printf("%d\n", ++counter);}
#endif
}
int main() {
int i;
pthread_t threads[2];
for (i = 0 ; i < 10; i++)
pthread_create(&threads[i], NULL, worker, NULL);
for (i = 0 ; i < 10; i++)
pthread_join(threads[i], NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment