-
-
Save OlivierNicole/20c6d58de1b773174a9bc093afb0cc09 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 <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdatomic.h> | |
#include <threads.h> | |
#include <time.h> | |
_Atomic(uint64_t) v = 0; | |
int f1(void* x) { | |
(void)x; | |
atomic_store_explicit(&v, 10L, memory_order_release); | |
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec | |
return 0; | |
} | |
int f2(void* x) { | |
(void)x; | |
(void)atomic_load_explicit(&v, memory_order_acquire); | |
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec | |
return 0; | |
} | |
int main() { | |
thrd_t t1, t2; | |
if(thrd_create(&t1, (thrd_start_t)f1, NULL) != thrd_success) | |
return EXIT_FAILURE; | |
if(thrd_create(&t2, (thrd_start_t)f2, NULL) != thrd_success) | |
return EXIT_FAILURE; | |
thrd_join(t1, NULL); | |
thrd_join(t2, NULL); | |
printf("v = %ld\n", v); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment