ofThread issue: Not working properly

Why this is behaving like this:

Thread class:

#include "ofMain.h"
class CmdThread : public ofThread {
public:
	CmdThread(){}
	void threadedFunction(){
		cout << "it is on!" << endl;
	}
};

testApp.cpp:

void testApp::setup(){
    CmdThread cmd;	
    cmd.startThread(true, false); // this gives me no output but this,
                                  // [warning] Thread 1: override threadedFunction with your own
}

And:

void testApp::setup(){
        CmdThread cmd;	
        cmd.startThread(true, false);
        cmd.startThread(true, false); // Now, this works and gives output,
                                      // it is on and ON!
                                      // [warning] Thread 1: cannot start, thread already 
                                      // running
}

The OF provided thread example is working fine. I’m using OF 0.80 VS2012.

What you’re doing there is creating a CmdThread object in testApp::setup(), but then it’s getting destroyed immediately when testApp::setup() exits. If you want it to continue to exist after setup() ends, add it to your testApp in the testApp.h file, like this:

class testApp : public ofBaseApp {
  ...
  CmdThread cmd;
};

and just do this in setup():

void testApp::setup() {
  cmd.startThread(true, false);
}
1 Like

It works! Thanks!