How To Create A New Thread In C++ - C++ Programming Tutorial
C++ Course / Multithreading / How To Create A New Thread In C++

How To Create A New Thread In C++

BLUF: Mastering How To Create A New Thread In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Create A New Thread In C++

C++ is renowned for its efficiency. Learn how How To Create A New Thread In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, initiating fresh threads presents a robust method to leverage multiple processors or cores and optimize program efficiency. Threads facilitate the simultaneous execution of multiple autonomous processes, empowering programs to handle numerous tasks concurrently. This capability proves especially beneficial for applications that heavily rely on CPU resources, like scientific or mathematical calculations, or those with substantial input/output operations.

Starting a fresh thread in C++ requires various actions such as specifying the thread function, establishing a thread object, and initiating the thread execution. This guide will delve into each of these procedures extensively.

Defining the Thread Function

The initial stage in starting a fresh thread involves specifying the function that will run within the thread. This function should execute the necessary tasks aligned with the thread's purpose. For instance, if the thread is supposed to handle computations or data processing, the function should encompass the appropriate code segments.

The function must adhere to a precise signature that aligns with the specifications of the thread library. Typically in C++, this entails the function accepting a void pointer as its parameter and yielding a void pointer as its return value. This setup enables the function to accept input data and deliver output outcomes through pointers.

Here is a sample of a thread function that calculates the total of a set of integer values:

Program 1

Example

#include <iostream>

const int ARRAY_SIZE = 5;

void* sum_array(void* arg) {
    int* array = static_cast<int*>(arg);
    int sum = 0;
    for (int i = 0; i < ARRAY_SIZE; i++) {
        sum += array[i];
    }
    return static_cast<void*>(new int(sum));
}

int main() {
    int array[ARRAY_SIZE] = {1, 2, 3, 4, 5};
    void* result = sum_array(static_cast<void*>(array));
    int* result_int = static_cast<int*>(result);
    std::cout << "The sum of the array is: " << *result_int << std::endl;
    delete result_int;
    return 0;
}

Output:

Explanation:

In this instance, the function receives a void pointer as its parameter and converts it to an array of integers. Following this, it calculates the total of all array elements and provides the outcome as a void pointer.

Creating a Thread Object

Once the thread function has been specified, the subsequent action involves generating a thread instance. In C++, this process is commonly achieved utilizing the std::thread class, a component of the C++ Standard Library.

To instantiate a fresh thread object, you must provide the thread function along with any necessary arguments when invoking the std::thread constructor. Below is a demonstration illustrating the creation of a new thread object for the previously outlined sum_array function:

Program 2

Example

#include <iostream>
#include <thread>

const int ARRAY_SIZE = 10;

void* sum_array(void* arg) {
    int* array = static_cast<int*>(arg);
    int sum = 0;
    for (int i = 0; i < ARRAY_SIZE; i++) {
        sum += array[i];
    }
    return static_cast<void*>(new int(sum));
}

int main() {
    int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::thread t(sum_array, static_cast<void*>(array));
    t.join();
    void* result = t.native_handle();
    int* result_int = static_cast<int*>(result);
    std::cout << "The sum of the array is: " << *result_int << std::endl;
    delete result_int;
    return 0;
}

Output:

Explanation:

In this instance, we instantiate a fresh integer array and provide it as an input to the sumarray function. Subsequently, we initialize a new std::thread instance, passing the sumarray function and the array parameter to the constructor.

Executing the Thread

After initializing the thread object, the subsequent action involves running the thread. This is achieved by invoking the std::thread::join function, initiating the thread's execution and pausing until it finishes.

Program 3

Example

int main() {
    int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::thread t(sum_array, static_cast<void*>(array));
    t.join();
    void* result = t.native_handle();
    int* result_int = static_cast<int*>(result);
    std::cout << "The sum of the array is: " << *result_int << std::endl;
    delete result_int;
    return 0;
}

Output:

Explanation:

In this instance, a fresh thread object is instantiated, followed by invoking the join method on the object. This action initiates the thread's execution and halts the program's progression until the thread finishes its task.

It is crucial to understand that the join function is limited to a single invocation on a thread object. To repeat the execution of the same thread function, a new thread object must be instantiated for each iteration.

Conclusion

When implementing new threads in C++, it can greatly enhance program efficiency, but it is crucial to do so thoughtfully. Threads consume extra memory and processing power, and overusing them may cause performance decline and related complications. Moreover, threads can bring about synchronization and concurrency challenges that need precise handling to prevent race conditions and similar issues.

In general, initiating fresh threads in C++ demands thoughtful preparation and implementation. Nonetheless, adhering to top-notch standards for thread architecture and supervision empowers developers to unleash the complete capabilities of multi-core processors and attain peak program efficiency.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience