1

Can B thread can created in A thread?

After waiting for B thread end, Can A thread continue to run?

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    Are you aware of the `detach` and `join` actions and how the lifetime of a thread *object* differs from the lifetime of the thread itself? It may be profitable to read https://stackoverflow.com/questions/5874493/why-is-destructor-of-boostthread-detaching-joinable-thread-instead-of-calling – o11c Jul 07 '15 at 06:41

3 Answers3

1

Short answer

  1. Yes
  2. Yes

There is very little conceptual difference between thread A and the main thread. Note that you could even join thread B in the main thread even though it was created from thread A.

Sample: (replace <thread> with <boost/thread.hpp> if you don't have a c++11 compiler yet)

Live On Coliru

#include <thread>
#include <iostream>

void threadB() {
    std::cout << "Hello world\n";
}

void threadA() {
    std::thread B(threadB);
    B.join();

    std::cout << "Continued to run\n";
}

int main() {
    std::thread A(threadA);
    A.join(); // no difference really
}

Prints

Hello world
Continued to run
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Try doing some intensive work and then printing the cout. I don't think this example can guarantee this scheduling if the two threads have any intensive work or I/O. – Matt Jul 07 '15 at 06:47
  • The question is not about scheduling. Indeed if you have independent background threads, you'd usually create them all from the same "master" run (and have a task queue of sorts to distribute the work) – sehe Jul 07 '15 at 06:48
  • I'm not sure it isn't. He mentions a specific order in which the threads run. You cant entail that his use case is a short as this example. Otherwise this is a good stepping stone to getting the job done. – Matt Jul 07 '15 at 06:49
  • @Matt are you posting on the right question? In sofar as the OP mentions sequence, my sample guarantees this. (Thread A continues to run after thread B exited). And I didn't assume we know anything about the OP's "use case". He had two very restricted questions about thread creation. – sehe Jul 07 '15 at 06:50
  • (After waiting for B thread end) This implies A waits for the whole of B. – Matt Jul 07 '15 at 06:51
  • And it does. Read [more closely](http://en.cppreference.com/w/cpp/thread/thread/join). – sehe Jul 07 '15 at 06:51
  • I'm just saying only in this example does it schedule in this way. A lot of the time I use threads I don't just call join on the parent right away. Otherwise why not just make a function call? – Matt Jul 07 '15 at 06:55
0

If B is a child thread of A?

There are ways to synchronize threads for turn taking. Whether or not they can run in parallel depends on using kernel threads or user threads. User threads are not aware of different processors so they cannot run truly in 'parallel'. If you want the threads to take turns you can use a mutex/semaphore/lock to synchronize them. If you want them to run in true parallel you will need B to be a child process of A.

You can also end the child thread/process in which case the parent will be scheduled. It's often not possible to guarantee scheduling without some sort of synchronization.

Matt
  • 3,592
  • 5
  • 21
  • 26
  • 1
    Thanks for your reply. – gongjl kevin Jul 07 '15 at 06:53
  • but I meet some problems about thread synchronize although I have done as you mentioned, pls see my issue. – gongjl kevin Jul 07 '15 at 06:56
  • It's tricky to synchronize threads b/c you have to worry about deadlocking them. Each thread is waiting for the other. It is effective to use timeouts on the threads and design some kind of resync mechanism. – Matt Jul 07 '15 at 06:57
  • void FuncA() { if(ScanResultsMonitorThread == NULL) { /* start thread A */ } } void FunAThread() { while(1) { FuncB(); } } void FuncB() { try { boost::this_thread::sleep(boost::posix_time::seconds(25)); } catch(const boost::thread_interrupted&) { } if(needRestart){ /* create thread B */ boost::thread Restart(&FuncBThread,this); boost::this_thread::sleep(boost::posix_time::seconds(10)); /* program can not run here and thread A end, why? */ } else { } } – gongjl kevin Jul 07 '15 at 07:14
  • Well it would be nice if you put the code into the website properly, but calling sleep is weird for one. – Matt Jul 07 '15 at 07:18
0

void FuncA() {

    if(ScanResultsMonitorThread == NULL) {
    /* start thread A */
    }

}

void FunAThread() {

    while(1) {
        FuncB();
    }

}

void FuncB() {

try {
    boost::this_thread::sleep(boost::posix_time::seconds(25));
}
catch(const boost::thread_interrupted&) {

}

if(needRestart){
    /* create thread B */ 
    boost::thread Restart(&FuncBThread,this);
    boost::this_thread::sleep(boost::posix_time::seconds(10));
    /* program can not run here and thread A end, why? */ 
}
else {

}

}

  • from the codes view, why thread A also is terminated ,after thread B end? – gongjl kevin Jul 07 '15 at 07:47
  • Sleeping the parent thread is not good. You should block the parent thread. Maybe thread B never comes back? I'm not familiar with the boost libraries otherwise I might be more help. – Matt Jul 07 '15 at 15:16
  • Thanks for you point out that, I have fixed my issue, yes, it should not sleep in the parent thread,I use time_joined() to wait for child thread end in parent thread,which fix the issue. – gongjl kevin Jul 08 '15 at 08:38