Multithreading is a technique where a program is divided into smaller units of execution called threads. Each thread runs independently but shares resources like memory, allowing tasks to be performed simultaneously. This helps improve performance by utilizing multiple CPU cores efficiently. Multithreading support was introduced in C++11 with the introduction of <thread> header file.
Create a Thread
The std::thread class represent the thread. Threading an instance of this class will create a thread with the given callable as its task.
C++
thread thread_name(callable);
where,
- thread_name: It is object of thread class.
- callable: It is a callable object like function pointer, function object.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to be run by the thread
void func() {
cout << "Hello from the thread!" << endl;
}
int main() {
// Create a thread that runs
// the function func
thread t(func);
// Main thread waits for 't' to finish
t.join();
cout << "Main thread finished.";
return 0;
}
Output
Hello from the thread!
Main thread finished.
A callable (such as a function, lambda, or function object) is passed to a thread. The callable is executed in parallel by the thread when it starts. like, thread t(func); creates a thread that runs the func function. We can also pass parameters along with callable, like this thread t(func, param1, param2);
In C++, callable can be divided into 4 categories:
- Function
- Lambda Expression
- Function Object
- Non-Static or static Member Function
Function Pointer
A function can be a callable object to pass to the thread constructor for initializing a thread.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to be run
// by the thread
void func(int n) {
cout << n;
}
int main() {
// Create a thread that runs
// the function func
thread t(func, 4);
// Wait for thread to finish
t.join();
return 0;
}
Output
4
Lambda Expression
Thread object can also be using a lambda expression as a callable. We can pass directly inside the thread object.
C++
#include <iostream>
#include <thread>
using namespace std;
int main() {
int n = 3;
// Create a thread that runs
// a lambda expression
thread t([](int n){
cout << n;
}, n);
// Wait for the thread to complete
t.join();
return 0;
}
Output
3
Function Objects
Function Objects or Functors can also be used for a thread as callable. To make functors callable, we need to overload the operator parentheses operator ().
C++
#include <iostream>
#include <thread>
using namespace std;
// Define a function object (functor)
class SumFunctor {
public:
int n;
SumFunctor(int a) : n(a) {}
// Overload the operator() to
// make it callable
void operator()() const {
cout << n;
}
};
int main() {
// Create a thread using
// the functor object
thread t(SumFunctor(3));
// Wait for the thread to
// complete
t.join();
return 0;
}
Output
3
Non-Static and Static Member Function
We can also use thread using the non-static or static member functions of a class. For non-static member function, we need to create an object of a class but it's not necessary with static member functions.
C++
#include <iostream>
#include <thread>
using namespace std;
class MyClass {
public:
// Non-static member function
void f1(int num) {
cout << num << endl;
}
// Static member function that takes one parameter
static void f2(int num) {
cout << num;
}
};
int main() {
// Member functions
// requires an object
MyClass obj;
// Passing object and parameter
thread t1(&MyClass::f1, &obj, 3);
t1.join();
// Static member function can
// be called without an object
thread t2(&MyClass::f2, 7);
// Wait for the thread to finish
t2.join();
return 0;
}
Output
3
7
Thread Management
In C++ thread library, various functions are defined to manage threads that can be reused to perform multiple tasks. Some of the are listed below:
Classes/Methods | Description |
---|
join() | It ensures that the calling thread waits for the specified thread to complete its execution. |
detach() | Allows the thread to run independently of the main thread, meaning the main thread does not need to wait. |
mutex | A mutex is used to protect shared data between threads to prevent data races and ensure synchronization. |
lock_guard | A wrapper for mutexes that automatically locks and unlocks the mutex in a scoped block. |
condition_variable | Used to synchronize threads, allowing one thread to wait for a condition before proceeding. |
atomic | Manages shared variables between threads in a thread-safe manner without using locks. |
sleep_for() | Pauses the execution of the current thread for a specified duration. |
sleep_until() | Pauses the execution of the current thread until a specified time point is reached. |
hardware_concurrency() | Returns the number of hardware threads available for use, allowing you to optimize the use of system resources. |
get_id | Retrieves the unique ID of the current thread, useful for logging or debugging purposes. |
Problems with Multithreading
Multithreading improves the performance and utilization of CPU, but it also introduces various problems:
- Deadlock
- Race Condition
- Starvation
Deadlock
A deadlock occurs when two or more threads are blocked forever because they are each waiting for shared resources that the other threads hold. This creates a cycle of waiting, and none of the threads can proceed.
Race Condition
A race condition occurs when two or more threads access shared resources at the same time, and at least one of them modifies the resource. Since the threads are competing to read and write the data, the final result depends on the order in which the threads execute, leading to unpredictable or incorrect results.
Starvation
Starvation occurs when a thread is continuously unable to access shared resources because other threads keep getting priority, preventing it from executing and making progress.
Thread Synchronization
In multithreading, synchronization is the way to control the access of multiple threads to shared resources, ensuring that only one thread can access a resource at a time to prevent data corruption or inconsistency. This is typically done using tools like mutexes, locks, and condition variables.
What is a context switch in multithreading?
Context switch is a process in multithreading which stores the state of the running thread so that it can be restored later, while the CPU switches to another thread for execution.
Similar Reads
C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why
5 min read
Introduction to c++
Difference between C and C++C++ is often viewed as a superset of C. C++ is also known as a "C with class" This was very nearly true when C++ was originally created, but the two languages have evolved over time with C picking up a number of features that either weren't found in the contemporary version of C++ or still haven't m
3 min read
Setting up C++ Development EnvironmentC++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an enviro
8 min read
Header Files in C++C++ offers its users a variety of functions, one of which is included in header files. In C++, all the header files may or may not end with the ".h" extension unlike in C, Where all the header files must necessarily end with the ".h" extension. Header files in C++ are basically used to declare an in
6 min read
Namespace in C++Name conflicts in C++ happen when different parts of a program use the same name for variables, functions, or classes, causing confusion for the compiler. To avoid this, C++ introduce namespace.Namespace is a feature that provides a way to group related identifiers such as variables, functions, and
6 min read
Writing First C++ Program - Hello World ExampleThe "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu
4 min read
Basics
C++ Data TypesData types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
C++ VariablesIn C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable
4 min read
Operators in C++C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.Example:C++#include <iostream> using namespace std; int main() { int a = 10 + 20; cout << a; return 0; }Outpu
9 min read
Basic Input / Output in C++In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.Output Stream: If the direction of flow of bytes
5 min read
Control flow statements in ProgrammingControl flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow St
15+ min read
C++ LoopsIn C++ programming, sometimes there is a need to perform some operation more than once or (say) n number of times. For example, suppose we want to print "Hello World" 5 times. Manually, we have to write cout for the C++ statement 5 times as shown.C++#include <iostream> using namespace std; int
7 min read
Functions in C++A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
C++ ArraysIn C++, an array is a derived data type that is used to store multiple values of similar data types in a contiguous memory location.Arrays in C++Create an ArrayIn C++, we can create/declare an array by simply specifying the data type first and then the name of the array with its size inside [] squar
10 min read
Strings in C++In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s
5 min read
Core Concepts
Pointers and References in C++In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a
5 min read
new and delete Operators in C++ For Dynamic MemoryIn C++, when a variable is declared, the compiler automatically reserves memory for it based on its data type. This memory is allocated in the program's stack memory at compilation of the program. Once allocated, it cannot be deleted or changed in size. However, C++ offers manual low-level memory ma
6 min read
Templates in C++C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
Structures, Unions and Enumerations in C++Structures, unions and enumerations (enums) are 3 user defined data types in C++. User defined data types allow us to create a data type specifically tailored for a particular purpose. It is generally created from the built-in or derived data types. Let's take a look at each of them one by one.Struc
3 min read
Exception Handling in C++In C++, exceptions are unexpected problems or errors that occur while a program is running. For example, in a program that divides two numbers, dividing a number by 0 is an exception as it may lead to undefined errors.The process of dealing with exceptions is known as exception handling. It allows p
11 min read
File Handling through C++ ClassesIn C++, programs run in the computerâs RAM (Random Access Memory), in which the data used by a program only exists while the program is running. Once the program terminates, all the data is automatically deleted. File handling allows us to manipulate files in the secondary memory of the computer (li
8 min read
Multithreading in C++Multithreading is a technique where a program is divided into smaller units of execution called threads. Each thread runs independently but shares resources like memory, allowing tasks to be performed simultaneously. This helps improve performance by utilizing multiple CPU cores efficiently. Multith
5 min read
C++ OOPS
Standard Template Library (STL)
Practice Problem