Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement