Created
April 19, 2012 23:19
-
-
Save dcolish/2424818 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 <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