Most of the times the main thread may execute in a shorter period of time than the child threads.
If the main thread completes before the child thread, then the child thread will be orphaned.
The main thread needs to wait for the child thread to complete.
We need to call t1.join() on the thread instance to make the main thread wait till t1 execution is complete.
Also we need to check if a thread is joinable (thread with active code of execution), before calling join.
Showing posts with label Multithreading. Show all posts
Showing posts with label Multithreading. Show all posts
Thursday, 25 June 2020
Wednesday, 24 June 2020
Multithreading :1.Create thread
We will be using std::thread class to create threads.
std:thread is introduced in C++11.
We can start creating threads by including the thread header file. To create a thread, we need to pass the code that needs to be executed in the constructor of the thread.
As soon as the thread object is created, a new thread is launched, which will execute the code passed to the constructor.
The constructor takes
std:thread is introduced in C++11.
We can start creating threads by including the thread header file. To create a thread, we need to pass the code that needs to be executed in the constructor of the thread.
As soon as the thread object is created, a new thread is launched, which will execute the code passed to the constructor.
The constructor takes
- a function object
- a function pointer
- a lambda expression
Subscribe to:
Posts (Atom)