Welcome to our deep dive into C++'s std::thread! In this lesson, we'll explore how to create and manage multiple threads in your C++ programs. Let's get started! š
A thread is a lightweight sub-process that can execute within a single program. It's like having multiple processes running concurrently in the same environment. This allows us to perform multiple tasks simultaneously, improving efficiency and performance.
std::thread šÆIn C++, the std::thread class allows us to create and manage threads. Here's an overview of its main functions:
std::thread::thread(function<void()> f): Creates a new thread that executes the provided function when started.std::thread::join(): Waits for the thread to complete execution.std::thread::detach(): Detaches the thread from its controlling object and continues execution in the background.Let's create a simple thread that prints "Hello, World!" to the console.
#include <iostream>
#include <thread>
void printHello() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
std::thread helloThread(printHello); // Create a new thread
helloThread.join(); // Wait for the thread to finish
return 0;
}š Note: Replace printHello() with your custom function.
We can create multiple threads and manage them using std::thread::join() to wait for their completion.
#include <iostream>
#include <thread>
#include <vector>
std::vector<std::thread> threads;
void printHello() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
for (int i = 0; i < 10; ++i) {
threads.push_back(std::thread(printHello)); // Create a new thread
}
for (auto& thread : threads) {
thread.join(); // Wait for each thread to finish
}
return 0;
}š Note: This example creates 10 threads that print "Hello, World!" concurrently.
Detaching a thread means it will continue executing in the background even after the controlling object is destroyed.
#include <iostream>
#include <thread>
#include <vector>
std::vector<std::thread> threads;
void printHello() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
for (int i = 0; i < 10; ++i) {
threads.push_back(std::thread(printHello)); // Create a new thread
threads[i].detach(); // Detach the thread
}
return 0;
}š Note: In this example, we detach the threads, which means they will continue running even after the program has finished executing.
We can pass arguments to our thread functions by using a lambda function.
#include <iostream>
#include <thread>
#include <vector>
std::vector<std::thread> threads;
void printMessage(const std::string& message) {
std::cout << message << std::endl;
}
int main() {
for (int i = 0; i < 10; ++i) {
auto argument = std::to_string(i);
threads.push_back(std::thread([argument]() {
printMessage("Thread " + argument + " starts...");
// Your code here
printMessage("Thread " + argument + " ends.");
}));
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}š Note: This example creates 10 threads and passes a unique argument to each thread.
What is the purpose of `std::thread` in C++?
We've covered the basics of C++'s std::thread. Now you can write multithreaded programs that can handle multiple tasks concurrently. Happy coding! š¤š