CK_PR_FENCE_STORE_ATOMIC(3)

πŸ’… ck_pr_fence_store_atomic β€” bestie, it's time to fence

okay so real talk: this is a page from the Concurrency Kit manual, section 3, and it's all about making sure your stores don't get reordered into your atomic read-modify-writes. no cap. if you're here, you're probably deep in the atomics sauce already, so let's go πŸ’–


πŸ“š LIBRARY

Concurrency Kit β€” that's libck, and you link it with -lck. yes queen, that's the whole vibe.


✨ SYNOPSIS ✨

#include <ck_pr.h>

void
ck_pr_fence_store_atomic(void);

void
ck_pr_fence_strict_store_atomic(void);

two functions, both void, both iconic in their own way πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so ck_pr_fence_store_atomic() enforces the ordering of any memory store, of ck_pr_store() calls, and of atomic read-modify-write operations β€” all of those get ordered relative to atomic read-modify-write operations, and that ordering is measured relative to the moment you invoke this function. periodt.

this function always serves as an implicit compiler barrier. like, no matter what, the compiler is not gonna sneak past it πŸ™ˆ

here's the tea though: this function will emit a fence for PSO and RMO targets. so if you're on one of those and you need the fence to definitely be emitted, you don't use this one β€” you use ck_pr_fence_strict_store_atomic() to force the emission of a fence. strict version, no vibes-based ambiguity πŸ’…


πŸ”₯ EXAMPLE πŸ”₯

#include <ck_pr.h>

static int a = 0;
static int b = 0;

void
function(void)
{

        ck_pr_store_int(&a, 1);

        /*
         * Guarantee that the store to a is completed
         * with respect to the update of b.
         */
        ck_pr_fence_store_atomic();
        ck_pr_add_int(&b, 2);
        return;
}

see how the fence sits right there between the store to a and the add on b? that's the whole point bestie, it's giving "guarantee that the store to a is completed with respect to the update of b" πŸ’–


🫢 RETURN VALUES 🫢

this function has no return value. it returns void. it simply does its job and leaves. main character energy, honestly. πŸ‘‘


πŸ‘€ SEE ALSO

and if you want even more lore, additional information is available at http://concurrencykit.org/ 🌈


ngl, if you're staring at a reordering bug at 3am, this page is your bestie. read it, love it, fence responsibly 🧚

mwah πŸ’‹ that's a wrap! this little page came to you from the Concurrency Kit corner of the world, dated May 18, 2013, and it lives in section CK_PR_FENCE_STORE_ATOMIC(3). stay atomic, stay slay, and don't forget to #include <ck_pr.h> πŸŽ€βœ¨πŸ’œ

πŸ“¦ where this page comes from