Created
March 22, 2016 05:20
-
-
Save wtshm/dbe6bc59776f5003fbc3 to your computer and use it in GitHub Desktop.
GCD on openFrameworks
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 "ofMain.h" | |
class ofApp : public ofBaseApp { | |
public: | |
string str = ""; | |
void setup() { | |
ofBackground(0); | |
ofSetFrameRate(60); | |
ofSetVerticalSync(true); | |
} | |
void update() { | |
} | |
void draw() { | |
ofDrawBitmapString(str, 15, 30); | |
} | |
void keyReleased(int key) { | |
str = ""; | |
if (key == '1') { | |
// run method from main thread | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
str = "hello from main thread"; | |
}); | |
} | |
if (key == '2') { | |
// run method from background thread | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
str = "hello from background thread"; | |
}); | |
} | |
if (key == '3') { | |
// run method from main thread after delay | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ | |
str = "hello from main thread"; | |
}); | |
} | |
if (key == '4') { | |
// run method from background thread after delay | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
str = "hello from background thread"; | |
}); | |
} | |
} | |
}; | |
int main() { | |
ofSetupOpenGL(320, 240, OF_WINDOW); | |
ofRunApp(new ofApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment