Created
September 22, 2013 19:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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