How to start a thread

Hi, guys.

I’m still not able to work with threads (C++ newbie here…). I couldn’t figure out its workings by looking at examples.
So, If I could get help to make it work on a simple situation like this:

Say I have a method called **printOut ** that prints “I am a secondary thread”. How do I make a call for this method, using the space bar?
So, everytime one hits the space bar, the printOut method gets called to run on a secondary thread and when done, the secondary thread is killed.

Thanks!

Alexandre

[quote author=“AlexandreRangel”]Hi, guys.

I’m still not able to work with threads (C++ newbie here…). I couldn’t figure out its workings by looking at examples.
So, If I could get help to make it work on a simple situation like this:

Say I have a method called **printOut ** that prints “I am a secondary thread”. How do I make a call for this method, using the space bar?
So, everytime one hits the space bar, the printOut method gets called to run on a secondary thread and when done, the secondary thread is killed.

Thanks!

Alexandre[/quote]

hi alexandre, Threads are actually OS dependant, I thought there was a general way but it seems there isn’t. Personally, i have done it in linux, so i could help you there.

but more importantly, if the example you are giving is similar to what you actually need, then you won’t need threads ata ll. why don’t you describe your problem in more detail?

you could also try the boost libs which i hear have thread support for different OSses: http://www.boost.org/doc/libs/1-35-0

Hi Alexandre,

there is a cross-platform thread addon provided with openFrameworks FAT. have a look at the ofxThread example, in particular the threadedObject.

the easiest way to share data between threads is with shared memory. you cannot call functions directly, so you set a state in some shared memory from one thread, and then the other thread reads that state. for example, in your case your threadedObject would have a bool spacebarWasPressed; which is normally false, but, when the spacebar is pressed, the main thread sets it to true, through a function like this:

  
void setSpacebarPressed() {  
  if ( lock() ) {  
    spacebarWasPressed = true;  
    unlock();  
  }  
}  
  

in your threadedObject thread, in the threadedFunction(), you’d do something like this:

  
void threadedFunction() {  
  while (isThreadRunning() != 0 ) {  
    if ( lock() ) {  
      if ( spacebarWasPressed ) {  
        printf("spacebar pressed\n");  
        spacebarWasPressed = false;  
      }  
      unlock();  
    }  
    ofSleepMillis(50);  
  }  
}  
  

hth
d
[/code]

Hi Ramy and Damian,

thanks a lot for the quick replies.

Rami, I’m working on Windows. Making this solution cros-platform would be great, thou.
I’ve tried to explain with a more simple example, but the real situation is this:
In my VJ app Quase-Cinema (first demo video here: http://www.youtube.com/watch?v=FGfAHV76-Nw), from time to time, he user needs to change the current video directory. For that, the users presses a key, and pronto… but the app freezes for a few moments, while ofxDirList lists the next directory. That’s where I think a new thread would come handy.

Damian, I’ll try that. A flag, like ChangeDir = 1 on the threadedFunction. When it’s 1, do the ofxDirList method.

Alexandre

[quote author=“AlexandreRangel”]Hi Rami and Damian,

thanks a lot for the quick replies.

Rami, I’m working on Windows. Making this solution cros-platform would be great, thou.
I’ve tried to explain with a more simple example, but the real situation is this:
In my VJ app Quase-Cinema (first demo video here: http://www.youtube.com/watch?v=FGfAHV76-Nw), from time to time, he user needs to change the current video directory. For that, the users presses a key, and pronto… but the app freezes for a few moments, while ofxDirList lists the next directory. That’s where I think a new thread would come handy.

Damian, I’ll try that. A flag, like ChangeDir = 1 on the threadedFunction. When it’s 1, do the ofxDirList method.

Alexandre[/quote]

yeah Damian’s idea is for sure the best. I didn’t know of_ had a thread add on, and for your problem, I think it’s what’s best.

Hi Damian,

I think I went half way towards the solution you proposed.

Here’s what I did:

  • On testApp.h :
  
#include "threadedObject.h"  
...  
bool            dirThread;  
threadedObject	TO;  
  

  • On my testApp.cpp testApp::setup :
  
TO.start();  
dirThread = false;  

  • On my testApp::keyPressed :
  
case 'e':  
if ( TO.lock() ) {  
   dirThread = true;  
   cout << "\ndirThread = " << dirThread;  
   TO.unlock();  
}  
break;  

  • On threadedObject.h
  
void threadedFunction(){  
			while( isThreadRunning() != 0 ){  
				if( lock() ){  
                    if ( dirThread == true ) { // !!! problem here !!!  
                        cout << "\n---Dir Thread running---\n";  
                        dirThread = false;  
  
                    }  
					unlock();  
					ofSleepMillis(50);  
				}  
			}  
		  

}

When I compile, the file threadedObject.h doen’t recoginize the dirThread variable coming from the testApp.cpp file.
Error: dirThread not declared in this scope.

Where did I not make it public, or something else wrong?