File tree Expand file tree Collapse file tree 5 files changed +30
-0
lines changed
examples/threads/threadExample/src Expand file tree Collapse file tree 5 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 44void ofApp::setup (){
55 threadedObject.setup ();
66 doLock = false ;
7+ ofLogNotice (" main thread Id" ) << ofGetMainThreadId ();
78}
89
910// --------------------------------------------------------------
@@ -39,6 +40,12 @@ void ofApp::draw(){
3940// --------------------------------------------------------------
4041void ofApp::keyPressed (int key){
4142 if (key == ' a' ){
43+ if (ofIsCurrentThreadTheMainThread ()) {
44+ ofLogNotice (" ofApp::keyPressed" ) << " processed in main thread" ;
45+ } else {
46+ // will never happen but to document the branch:
47+ ofLogNotice (" ofApp::keyPressed" ) << " processed in other thread" ;
48+ }
4249 threadedObject.start ();
4350 }else if (key == ' s' ){
4451 threadedObject.stop ();
Original file line number Diff line number Diff line change @@ -51,6 +51,12 @@ class ThreadedObject: public ofThread
5151 // / other tasks.
5252 void threadedFunction (){
5353 while (isThreadRunning ()){
54+ if (ofIsCurrentThreadTheMainThread ()) {
55+ // will never happen but to document the branch:
56+ ofLogNotice (" ThreadedObject::threadedFunction" ) << " processed in main thread" ;
57+ } else {
58+ ofLogNotice (" ThreadedObject::threadedFunction" ) << " processed in other thread" ;
59+ }
5460 // since we are only writting to the frame number from one thread
5561 // and there's no calculations that depend on it we can just write to
5662 // it without locking
Original file line number Diff line number Diff line change @@ -323,6 +323,15 @@ ofBaseApp * ofGetAppPtr(){
323323 return mainLoop ()->getCurrentApp ().get ();
324324}
325325
326+ // --------------------------------------
327+ std::thread::id ofGetMainThreadId () {
328+ return ofGetMainLoop ()->get_thread_id () ;
329+ }
330+
331+ bool ofIsCurrentThreadTheMainThread () {
332+ return ofGetMainThreadId () == std::this_thread::get_id ();
333+ }
334+
326335// --------------------------------------
327336ofAppBaseWindow * ofGetWindowPtr (){
328337 return mainLoop ()->getCurrentWindow ().get ();
Original file line number Diff line number Diff line change @@ -52,6 +52,9 @@ ofBaseApp * ofGetAppPtr();
5252
5353void ofExit (int status=0 );
5454
55+ bool ofIsCurrentThreadTheMainThread ();
56+ std::thread::id ofGetMainThreadId ();
57+
5558// -------------------------- time
5659float ofGetFrameRate ();
5760float ofGetTargetFrameRate ();
Original file line number Diff line number Diff line change @@ -46,7 +46,12 @@ class ofMainLoop {
4646
4747 ofEvent<void > exitEvent;
4848 ofEvent<void > loopEvent;
49+
50+ std::thread::id get_thread_id () { return thread_id; };
51+
4952private:
53+ std::thread::id thread_id { std::this_thread::get_id () };
54+
5055 void keyPressed (ofKeyEventArgs & key);
5156 std::unordered_map<std::shared_ptr<ofAppBaseWindow>, std::shared_ptr<ofBaseApp> > windowsApps;
5257 bool bShouldClose;
You can’t perform that action at this time.
0 commit comments