Created
May 9, 2019 23:41
-
-
Save marcopeereboom/4357c59b57dc998a58d37817d59f99b0 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 | |
#include | |
#include | |
#include | |
#define NCOUNT (100000) | |
#define TCOUNT (4) | |
volatile uint64_t u = 0; | |
volatile uint64_t *casvar = &u; | |
void * | |
thread(void *args) { | |
volatile uint64_t val = 0; | |
int i; | |
for (i = 0; i < NCOUNT; i++) { | |
for (;;) { | |
__atomic_load(casvar, &val, __ATOMIC_CONSUME); | |
if (__sync_val_compare_and_swap(casvar, val, val+1) == val) | |
break; | |
} | |
} | |
return (NULL); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
uint64_t i; | |
pthread_t tid[TCOUNT]; | |
volatile uint64_t result; | |
for (i = 0; i < TCOUNT; i++) | |
pthread_create(&tid[i], NULL, thread, (void *)i+10); | |
for (i = 0; i < TCOUNT; i++) { | |
if (pthread_join(tid[i], NULL)) | |
printf("pthread_join failed %d\n", i+10); | |
} | |
__atomic_load(casvar, &result, __ATOMIC_CONSUME); | |
if (result != TCOUNT*NCOUNT) { | |
printf("womp womp %d %d\n", result, TCOUNT*NCOUNT); | |
return (1); | |
} | |
printf("ok %d %d\n", result, TCOUNT*NCOUNT); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment