C++ Multithreading

By | March 15, 2020

Threading generally represents a part of the process which is at running state, in C++ we get the feature of multi-threading which allows us to perform multitasking which means we can run two or more than two programs concurrently.

There are two types of Multi-tasking:

  • Process-based Multi-tasking
  • Thread-based Multi-tasking

Process-Based Multitasking

The process-based multitasking concerned with the complete execution of the concurrent program.

Thread-Based Multitasking

While the thread-based multitasking deal with the execution of the various pieces of the same program.

When multithreading runs multiple processes, it generally contains two or more parts of the program and these parts are known as threads and each thread has a different path for execution.

The C++ 11 and above version provide us with std::thread and this thread class and all its associative functions are defined in the thread header file. The prior version of C++ pas p thread library which has some issues but here we have used the thread library of C++ 11.

Creating a Thread

The std::thread is a thread class that is used to represent the single thread in C++. First using the std::thread class we create the object of the thread and pass the function or any other callable executing code into the constructor of the object.

Syntax to create Thread Object:

For Function pointer:
void func(args)
{
// function body
}
std::thread thread_obj(func, args);

For Lambda expression:

auto l = [](args) 
{
    //code
};
std::thread thread_object(l, args);

For class Objects:

class class_name 
{
    void operator()(args)
    {
        // overload operator block
    }
}

// Create thread object of class
std::thread thread_object(fn_class_object(), params)

Once the compiler encoder the thread creating statement it might take time to perform some action at the background so until the thread finishes its work, we need to wait for it.

The example we have provided here is small so you would not have to wait for the thread to perform any action.

In big programs we use the std::thread::join() function which halts the program execution for some seconds so the thread can be finished and the user could not interrupt the program execution.

Syntax to wait for the thread

  // Start thread t
    std::thread t(callable);

    // Wait for t to finish
    t1.join();

Thread Example for Multithreading

NOTE: “To compile programs with std::thread support use g++ -std=c++11 -pthread”

#include <iostream>
#include <thread>
using namespace std;

void func(int n)
{
    for (int i = 0; i < n; i++) {
        cout << "Function Thread is called \n";
    }
}

class class_thread {
public:
    void operator()(int x)
    {
        for (int i = 0; i < x; i++)
            cout << "Class Thread is called\n";
    }
};

int main()
{
    thread f_t(func, 3);
    thread c_t(class_thread(), 3);

    // Wait for thread f_t to finish
    f_t.join();
    // Wait for thread c_t to finish
    c_t.join();  
    return 0;
}

Output:

Function Thread is called
Class Thread is called
Class Thread is called
Function Thread is called
Function Thread is called
Class Thread is called

Behind the code:

In this above example, we can see that the function and class statements are executing simultaneously.

Leave a Reply

Your email address will not be published. Required fields are marked *