Skip to content

Instantly share code, notes, and snippets.

@tbodt
Created February 2, 2020 05:06
Show Gist options
  • Save tbodt/d390c205d29f7d49e89597ec578f276a to your computer and use it in GitHub Desktop.
Save tbodt/d390c205d29f7d49e89597ec578f276a to your computer and use it in GitHub Desktop.
#include <pthread.h>
#define ATOMIC 1
int flags;
void *thread(void *asdf) {
(void) asdf;
for (;;) {
#if ATOMIC
__atomic_fetch_or(&flags, 1 << 3, __ATOMIC_SEQ_CST);
__atomic_fetch_and(&flags, ~(1 << 3), __ATOMIC_SEQ_CST);
#else
flags |= 1 << 3;
flags &= ~(1 << 3);
#endif
}
return NULL;
}
int main() {
pthread_t t;
pthread_create(&t, NULL, thread, NULL);
pthread_detach(t);
for (;;) {
#if ATOMIC
__atomic_fetch_or(&flags, 1 << 5, __ATOMIC_SEQ_CST);
__atomic_fetch_and(&flags, ~(1 << 5), __ATOMIC_SEQ_CST);
#else
flags |= 1 << 5;
flags &= ~(1 << 5);
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment