ZMQ_ATOMIC_COUNTER_I(3)

πŸ’… ZMQ_ATOMIC_COUNTER_I(3) β€” bestie edition ✨

okay so this is a page from the 0MQ manual (you know, the ZeroMQ girlies) and it's about ONE tiny little function that does ONE tiny little thing, but it does it flawlessly under pressure. main character energy fr. let's get into it 🫢


✨ NAME ✨

zmq_atomic_counter_inc β€” bestie, it increments an atomic counter. that's the whole job. she's simple, she's iconic, she's booked and busy. πŸ’–


🌸 SYNOPSIS 🌸

int zmq_atomic_counter_inc (void *counter);

literally one function. one arg. no drama. we love a low-maintenance queen. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so the zmq_atomic_counter_inc function increments an atomic counter in a threadsafe fashion. yes, threadsafe β€” meaning multiple threads can be fighting over this counter at the same time and she will NOT fumble. not on her watch. πŸ™ˆ

and how does she pull that off? this function uses platform specific atomic operations. so like, she's not doing anything sketchy or hand-wavy β€” she's using whatever the underlying platform gives her that's actually atomic. no cap. πŸ’…


πŸ’œ RETURN VALUE πŸ’œ

the zmq_atomic_counter_inc() function returns the old value of the counter, before incrementing.

read that again bestie. the OLD value. not the new one. not the value plus one. the value before she did the thing. this trips people up EVERY time and it's giving "gotcha" energy, so clock it now. ⭐


πŸŽ€ EXAMPLE πŸŽ€

test code for atomic counters. look at this little glow-up arc β€” she starts at 0, climbs to 3, dips down, gets set, dips again, and then gets destroyed. growth, then closure. very demure. very mindful. 🌈

void *counter = zmq_atomic_counter_new ();
assert (zmq_atomic_counter_value (counter) == 0);
assert (zmq_atomic_counter_inc (counter) == 0);
assert (zmq_atomic_counter_inc (counter) == 1);
assert (zmq_atomic_counter_inc (counter) == 2);
assert (zmq_atomic_counter_value (counter) == 3);
assert (zmq_atomic_counter_dec (counter) == 1);
assert (zmq_atomic_counter_dec (counter) == 1);
assert (zmq_atomic_counter_dec (counter) == 0);
zmq_atomic_counter_set (counter, 2);
assert (zmq_atomic_counter_dec (counter) == 1);
assert (zmq_atomic_counter_dec (counter) == 0);
zmq_atomic_counter_destroy (&counter);
return 0;

notice how every inc returns the value from before it incremented?? that's the whole point of the RETURN VALUE section bestie, i TOLD you. 😭


πŸ”₯ SEE ALSO πŸ”₯

her whole friend group, her squad, her support system β€” go read these too:

iykyk. the whole atomic counter cinematic universe. πŸ’€βœ¨


πŸ‘‘ AUTHORS πŸ‘‘

this page was written by the 0MQ community. to make a change please read the 0MQ Contribution Policy at http://www.zeromq.org/docs:contributing.

like yes, contribute bestie, the community WANTS you, go be part of the lore. 🫢


and that's the whole page! πŸ’– this one's from 0MQ 4.3.5, dated 10/09/2023 β€” so she's a little vintage but she still eats. periodt. πŸ“πŸ§šβ­

πŸ“¦ where this page comes from