0

So I have been using boost as a solution for threading. I seem to be having an issue where the threads I create dont let the main thread that was executing them continue. Eg:

int main(){
    while(1){
        speech listen; //create speech object
        boost::thread speech_thd(boost::bind(&speech::Run,&listen));
        speech_thd.join();
        std::cout<<"test\n";
        //Some sleep call here 
    }

The "test" call is only printed after speech_thd finishes execution. How would I create it such that I get execution on the main while(1) as well? If I can do this I will obviously move the thread creation outside the while(1) :P Thanks for any help!

bge0
  • 901
  • 2
  • 10
  • 25
  • Is this code correct? Is `speech Listen;` and the `&listen` on the next line the same object (is this a typo?), or are they different objects? – Wayne Uroda May 16 '13 at 06:24
  • Sorry caps issue. I was just using this as a demonstration. – bge0 May 16 '13 at 22:33

1 Answers1

2

Don't call join on the thread you just created - join specifically waits there in the main thread until the speech_thd terminates, see here: http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_join.html

Wayne Uroda
  • 5,025
  • 4
  • 30
  • 35
  • Will the thread start execution without a call to join()? Also, will this also work if I call the arg as follows: boost::thread(&listen::RecordMethod,this); ? [Note that this is called within the object Listen itself. – bge0 May 16 '13 at 04:31
  • 1
    Yes, the thread starts immediately upon construction. I am not sure about your second question - perhaps you should ask another question on SO. – Wayne Uroda May 16 '13 at 06:23