Skip to content

Instantly share code, notes, and snippets.

@wtshm
Created March 22, 2016 05:20
Show Gist options
  • Save wtshm/dbe6bc59776f5003fbc3 to your computer and use it in GitHub Desktop.
Save wtshm/dbe6bc59776f5003fbc3 to your computer and use it in GitHub Desktop.
GCD on openFrameworks
#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