Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Introduction to Multithreading in C++

What is Multithreading?

Multithreading is the ability of a CPU, or a single core in a multi-core processor, to provide multiple threads of execution concurrently, supported by the operating system. This is a very useful feature as it allows concurrent execution of two or more threads, which can improve the performance of applications.

Why Use Multithreading?

Multithreading can significantly improve the performance of a program by dividing tasks into smaller threads that can be executed simultaneously. This is especially useful for CPU-bound tasks. Benefits of multithreading include:

  • Increased responsiveness
  • Better resource utilization
  • Improved performance on multi-core systems

Basic Concepts

Before diving into multithreading in C++, it's important to understand a few basic concepts:

  • Thread: The smallest unit of execution within a process.
  • Process: An instance of a program in execution.
  • Concurrency: The ability to run multiple tasks at the same time.

Creating Threads in C++

In C++, the std::thread class is used to create and manage threads. Here's a simple example:


#include <iostream>
#include <thread>

void printMessage() {
    std::cout << "Hello from the thread!" << std::endl;
}

int main() {
    std::thread t(printMessage);
    t.join(); // Wait for the thread to finish
    return 0;
}
                

In this example, a new thread is created to run the printMessage function. The join method is used to wait for the thread to complete before exiting the main function.

Passing Arguments to Threads

You can pass arguments to threads in C++ as follows:


#include <iostream>
#include <thread>

void printNumber(int n) {
    std::cout << "Number: " << n << std::endl;
}

int main() {
    std::thread t(printNumber, 10);
    t.join();
    return 0;
}
                

In this example, the printNumber function takes an integer argument, and we pass the value 10 to it when creating the thread.

Using Lambda Functions

Lambda functions can be used for creating threads without defining separate functions:


#include <iostream>
#include <thread>

int main() {
    std::thread t([] {
        std::cout << "Hello from lambda!" << std::endl;
    });
    t.join();
    return 0;
}
                

This example creates a thread that runs a lambda function, which prints a message to the console.

Detaching Threads

Sometimes, you may want a thread to run independently from the main thread. This can be achieved using the detach method:


#include <iostream>
#include <thread>

void independentThread() {
    std::cout << "This is an independent thread!" << std::endl;
}

int main() {
    std::thread t(independentThread);
    t.detach(); // Detach the thread
    std::cout << "Main thread is free to do other work!" << std::endl;
    return 0;
}
                

In this example, the main thread is free to continue its execution after detaching the new thread. The detached thread will run independently.

Thread Synchronization

When multiple threads access shared resources, it is crucial to synchronize their actions to avoid data races. The std::mutex class is commonly used for this purpose:


#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void printMessage(const std::string& msg) {
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << msg << std::endl;
}

int main() {
    std::thread t1(printMessage, "Hello from thread 1");
    std::thread t2(printMessage, "Hello from thread 2");
    t1.join();
    t2.join();
    return 0;
}
                

In this example, a mutex is used to ensure that only one thread prints a message at a time, avoiding potential data races.

Conclusion

Multithreading is a powerful feature that can significantly improve the performance and responsiveness of applications. In this tutorial, we covered the basics of multithreading in C++, including creating threads, passing arguments, using lambda functions, detaching threads, and thread synchronization. With these concepts, you can start incorporating multithreading into your C++ programs to take full advantage of modern multi-core processors.