Created
May 11, 2019 06:25
-
-
Save 4a6f656c/8433032a3f70893a278259f8108aad90 to your computer and use it in GitHub Desktop.
cas-thread-gcc.c
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 <stdint.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
#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