Skip to content

Instantly share code, notes, and snippets.

@tbodt
Created February 2, 2020 05:06

Revisions

  1. tbodt created this gist Feb 2, 2020.
    33 changes: 33 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #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
    }
    }