Skip to content

Instantly share code, notes, and snippets.

@preshing
Created September 22, 2013 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preshing/6663087 to your computer and use it in GitHub Desktop.
Save preshing/6663087 to your computer and use it in GitHub Desktop.
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