Skip to content

Instantly share code, notes, and snippets.

@OlivierNicole
Created May 12, 2022 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlivierNicole/20c6d58de1b773174a9bc093afb0cc09 to your computer and use it in GitHub Desktop.
Save OlivierNicole/20c6d58de1b773174a9bc093afb0cc09 to your computer and use it in GitHub Desktop.
#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