Skip to content

Instantly share code, notes, and snippets.

@hiroyuki
Created February 13, 2020 01:20
Show Gist options
  • Save hiroyuki/b4a2ab18bcc01eb8bbe756a75a39cc09 to your computer and use it in GitHub Desktop.
Save hiroyuki/b4a2ab18bcc01eb8bbe756a75a39cc09 to your computer and use it in GitHub Desktop.
atomic release aquire simple study in openFrameworks
#pragma once
#include "ofMain.h"
class WorkerThread : public ofThread
{
public:
bool pinpon = false;
atomic_bool isFrameNew;
int value = 0;
void threadedFunction()
{
while (isThreadRunning())
{
if (!pinpon || !isFrameNew.load(memory_order_acquire))
{
value++;
ofLog() << "thread " << value;
isFrameNew.store(true, memory_order_release);
ofSleepMillis(15);
}
}
}
void update()
{
if (isFrameNew.load(memory_order_acquire))
{
ofLog() << "update" << value;
value--;
isFrameNew.store(false, pinpon ? memory_order_release : memory_order_relaxed);
}
}
~WorkerThread()
{
waitForThread(true);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment