Introduction
Since C++11, the standard library <thread> has supported multi-threaded programming at the language level. This allows you to implement parallel processing, such as performing heavy calculations in the background while keeping the user interface responsive.
The core of this functionality is the std::thread class. By creating an object of this class, you can start a new thread and execute a specified function independently of the main thread.
This article explains the most basic way to create a new thread and execute a simple task using std::thread.
Sample Code Using std::thread
This code executes the worker_task function in a new worker thread, separate from the main thread (the thread where the main function is running).
#include <iostream>
#include <thread> // Required to use std::thread
#include <chrono> // Required to use std::chrono
using namespace std;
// 1. Function to be executed in a new thread
void worker_task() {
cout << "Worker Thread: Starting process." << endl;
// Simulate time-consuming processing
this_thread::sleep_for(chrono::seconds(2));
cout << "Worker Thread: Process completed." << endl;
}
int main() {
cout << "Main Thread: Starting worker thread." << endl;
// 2. Pass the function you want to execute to the std::thread constructor to start the thread
thread worker(worker_task);
// The main thread can continue its own processing without waiting for the worker thread
cout << "Main Thread: Proceeding while the worker thread is running." << endl;
// 3. Wait for the worker thread to finish (Join)
worker.join();
cout << "Main Thread: Worker thread finished. Exiting program." << endl;
return 0;
}
Execution Result (Example)
Main Thread: Starting worker thread.
Main Thread: Proceeding while the worker thread is running.
Worker Thread: Starting process.
(About 2 seconds later)
Worker Thread: Process completed.
Main Thread: Worker thread finished. Exiting program.
Code Explanation
1. void worker_task()
This is the function that describes the processing you want to run in the new thread (worker thread). It is defined just like a normal function.
2. thread worker(worker_task);
This single line is the core part that creates a new thread and starts processing.
thread worker(...): Declares an objectworkerof typestd::thread.(worker_task): Passes the functionworker_taskyou want to execute as a constructor argument.
The moment this line is executed, the OS creates a new thread, and the execution of the worker_task function begins on that thread. The main function proceeds to the next line immediately without waiting for worker_task to complete.
3. worker.join();
.join() is an important method to wait (block) the execution of the main thread until the worker thread completes its processing. If the main function ends without calling join(), the entire program will terminate even if worker_task is in the middle of processing, and the worker thread will be forcibly interrupted.
The std::thread object must have either .join() (wait) or .detach() (separate) called before it is destroyed (goes out of scope).
Summary
In this article, I explained the basic method of creating a new thread using C++ std::thread.
- Include the
<thread>header. - Create an object by passing the function you want to execute to the
std::threadconstructor. - Finally, call
.join()on the created thread object to wait for the thread to finish.
std::thread is the first step to realizing parallel processing in C++. Mastering this basic concept opens the way to creating more advanced programs, such as responsive UI applications and speeding up calculation processing.
