jammy
jammy

Reputation: 977

Threading concepts c++

int main(){
std::thread t1(findEven, start, end, &(EvenSum));
    std::thread t2(findOdd, start, end, &(OddSum));
 
    t1.join();
    t2.join();
}

Assume that findEven and findOdd are some functions that has already been written . For better understandig , this is the code Link to Code

Having some difficulty in understanding threading concepts and wanted to check if my understanding is correct? My main thread created a new thread T1 and ran the function Evensum .It did not wait for evensum to finish and came back and again launched a new thread T2 and running findodd at same time .now both t1 and t2 are running at the same time .that's fine. now it hits t1.join () . Does it mean that the line t2.join() is not reached as long as t1 is not finished?

Upvotes: 0

Views: 302

Answers (1)

Yury Schkatula
Yury Schkatula

Reputation: 5369

Your understanding is totally correct.

  1. Thread invocation just triggers the thing and let it go independently (unless you provide extra options like "pending start" so you would activate created thread later upon demand).
  2. Once asked to join, current (main?) thread is suspended until associated thread is signaled as completed. In case of multiple threads, you may wait for all, or for any of if necessary.

Upvotes: 1

Related Questions