C++ - SPLessons

C++ Multithreading

Chapter 28

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Multithreading

C++ Multithreading

shape Definition

In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity.

An application consisting of process divided into multiple sub-process can be termed as “CPP Multithreading“. 

shape Description

The thread formed consists of small part of the original code to be executed and given clear instructions by the thread scheduler to perform the task. All the threads work simultaneously to improve the performance of the program.

In CPP Multithreading at a time many threads are formed from a process and the resources are allocated to them in parallel as per the requirement. Whereas, in single thread process, time and resources is divided among different process which reduces the efficiency of the program.

shape Conceptual
figure

Creating a thread

shape Description

The header file thread is used in C++, to use the built in multi-threads.

A thread is created by creating a thread class object. A thread is useless until it is initialized. No information is passed initially to a default thread created. There are two ways to initialize a thread.


Thread initialization using functions

shape Description

Function pointer points to the constructor of the function once the thread is created and continues to work independently by using the resources provided.

shape Example

#include <iostream>
#include <thread>
using namespace std;
//thread intialization using functions
void func()
{
	cout << "Welcome to splessons" << endl;

}
int main()
{
	//passing function to the thread
	thread Test(func);
}

In the above example, the program is compiled but run-time error occurs because even though main function execution completes, the thread Test function keeps on working as it is not terminated before the main function termination. The problem can be overcome by using joining threads.

Thread initialization using objects

shape Description

Thread can be created using objects with the help of function object also called as “functor“.

Functor is a class object which is obtained by overloading operator(). Thread initialization is done by passing the class object to the thread constructor.

shape Syntax

void operator()()

Here, the object parameters are placed in the second braces.

shape Example

#include<iostream>
#include<thread>
using namespace std;
class new_functor
{
public:
  void operator()()
   {
    cout<< "Welcome to splessons" << endl;
   }
};
int main()
{
new_functor func;
thread test(func);
if(test.joinable())
{
test.join();
}
return 0;
}

Output

Welcome to splessons

Thread joining

shape Description

While using the concept of thread joining three thread functions are used.

joinable()

joinable() function is used to check whether the thread function is ready to join the main thread. This function helps to check any errors while performing a task.

bool joinable()

boolean type is used because joinable function returns true or false.

join()

join() function is used to attach the child threads to the main thread. The threads are joined once the child class threads are executed.If the threads are joined, the child threads becomes not joinable and are not given access but can be destroyed without any efforts.

void join();

Here, join() returns only after the execution of all child threads. If execution is not completed, join() function waits.

detach()

Detached threads are also called Daemon/Background threads. To detach a thread detach() function is called on thread object.

thread th(funcPtr);
th.detach();

After calling detach(), thread object will be no longer associated with the actual thread of execution.

shape Example

 #include <iostream> 
#include <thread> 
using namespace std; //thread intialization using functions 
void func() 
{
	cout << "Welcome to splessons" << endl;
} 
int main() //main thread 
{ 
	//child thread thread Test(func); //verifying if thread is joinable 
	if (Test.joinable()) 
	{ 
		//main is blocked until Test is not finished 
		Test.join(); 
	} 
	//terminating the thread 
	else 
	{ 
		Test.detach(); 
	} 
}

Output

Welcome to splessons

Summary

shape Key Points

  • CPP Multithreading divides the process into sub parts.
  • A thread can be created using functions and objects.
  • joinable(), join() and detach() are thread joining techniques.
  • “functor” is the thread initializer.