C++ Threads (std::thread)
In this page:
Introduction to Threads
The 'std::thread' class in C++ allows you to run multiple parts of your program in parallel. This enables multitasking, which can improve your program's performance on multi-core processors.
Example: Introduction to Threads
#include <iostream>
#include <thread>
void task() { std::cout << "Running in a thread" << std::endl; }
int main() {
std::thread t(task); // runs in parallel
t.join();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Joining Threads
You must call join() on your thread object before it is destroyed. The join() function blocks the main program and waits for the thread to finish its work, ensuring a clean shutdown.
Example: Joining Threads
#include <iostream>
#include <thread>
void task() { std::cout << "Thread work" << std::endl; }
int main() {
std::thread t(task);
t.join(); // waits for the thread to finish
std::cout << "Main continues after join" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Detaching Threads
Alternatively, you can call detach() to let a thread run independently in the background. Once detached, you cannot rejoin the thread, and it will clean up after itself when it finishes.
Example: Detaching Threads
#include <iostream>
#include <thread>
#include <chrono>
void task() { std::cout << "Detached thread running" << std::endl; }
int main() {
std::thread t(task);
t.detach(); // runs independently, cannot be rejoined
std::this_thread::sleep_for(std::chrono::milliseconds(50));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Arguments to Threads
You can pass arguments to thread functions. By default, arguments are copied. If you want to pass an argument by reference, wrap it with 'std::ref'. Passing a reference without std::ref would actually copy the value, which is rarely what you intend when a thread needs to modify shared state.
Example: Passing Arguments to Threads
#include <iostream>
#include <thread>
#include <string>
void greet(std::string name) { std::cout << "Hello, " << name << std::endl; }
int main() {
std::thread t(greet, "Alex"); // argument copied by default
t.join();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Managing Thread IDs
Each thread has a unique identifier. You can find the current thread's ID using std::this_thread::get_id(), or use sleep_for() to pause execution. Comparing thread IDs is the standard way to check whether two std::thread objects refer to the same underlying OS thread.
Example: Managing Thread IDs
#include <iostream>
#include <thread>
int main() {
std::cout << "Main thread ID: " << std::this_thread::get_id() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced