Create a gist now

Instantly share code, notes, and snippets.

The example from http://preshing.com/20130922/acquire-and-release-fences, rewritten using Mintomic.
#include <mintomic/mintomic.h>
#include <time.h>
struct Message
{
clock_t tick;
const char* str;
void* param;
};
Message g_payload;
mint_atomic32_t g_guard;
void SendTestMessage(void* param)
{
// Copy to shared memory using non-atomic stores.
g_payload.tick = clock();
g_payload.str = "TestMessage";
g_payload.param = param;
// Release fence.
mint_thread_fence_release();
// Perform an atomic write to indicate that the message is ready.
mint_store_32_relaxed(&g_guard, 1);
}
bool TryReceiveMessage(Message& result)
{
// Perform an atomic read to check whether the message is ready.
int ready = mint_load_32_relaxed(&g_guard);
if (ready != 0)
{
// Acquire fence.
mint_thread_fence_acquire();
// Yes. Copy from shared memory using non-atomic loads.
result.tick = g_payload.tick;
result.str = g_msg_str;
result.param = g_payload.param;
return true;
}
// No.
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment