Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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