Skip to content

Instantly share code, notes, and snippets.

@fcamel
Created November 6, 2016 14:09
Show Gist options
  • Save fcamel/48c8f2f8f3835319645df18fefbc52e7 to your computer and use it in GitHub Desktop.
Save fcamel/48c8f2f8f3835319645df18fefbc52e7 to your computer and use it in GitHub Desktop.
// Source: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1525.htm
const int num_mailboxes = 32;
_Atomic volatile int mailbox[num_mailboxes];
void MailboxInspection(int my_id, void (*do_work)(int)) {
for (int i = 0; i < num_mailboxes; i++) {
if (atomic_load_explicit(&mailbox[i], memory_order_relaxed) == my_id) {
atomic_thread_fence(memory_order_acquire); // prevent speculative reads in do_work [7.17.4.1]
do_work(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment