Created
February 13, 2020 01:20
-
-
Save hiroyuki/b4a2ab18bcc01eb8bbe756a75a39cc09 to your computer and use it in GitHub Desktop.
atomic release aquire simple study in openFrameworks
This file contains hidden or 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
#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