Skip to content

Commit 8804b19

Browse files
authored
ofIsCurrentThreadTheMainThread (#7548)
#changelog #thread
1 parent 67e0b87 commit 8804b19

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

examples/threads/threadExample/src/ofApp.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
void 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
//--------------------------------------------------------------
4041
void 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();

examples/threads/threadExample/src/threadedObject.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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

libs/openFrameworks/app/ofAppRunner.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
//--------------------------------------
327336
ofAppBaseWindow * ofGetWindowPtr(){
328337
return mainLoop()->getCurrentWindow().get();

libs/openFrameworks/app/ofAppRunner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ ofBaseApp * ofGetAppPtr();
5252

5353
void ofExit(int status=0);
5454

55+
bool ofIsCurrentThreadTheMainThread();
56+
std::thread::id ofGetMainThreadId();
57+
5558
//-------------------------- time
5659
float ofGetFrameRate();
5760
float ofGetTargetFrameRate();

libs/openFrameworks/app/ofMainLoop.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
4952
private:
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;

0 commit comments

Comments
 (0)