This tutorial will take you through the process of creating and managing threads in C++. Threads are a way for a program to divide itself into two or more concurrently running tasks. They allow programs to do more than one thing at a time.
By the end of this tutorial, you will learn how to:
- Create threads in C++
- Stop and control threads
- Avoid common pitfalls
Prerequisites: Familiarity with C++ syntax and basic knowledge of how functions work in C++.
C++ Standard Library provides support for threading via <thread>
, <mutex>
, <condition_variable>
and other related headers. A thread is created by constructing a std::thread
object and providing the function you want the system to run as a separate thread.
To create a thread, you need to construct a std::thread
object and pass it a function:
#include <thread>
void my_function() {
// code to be executed by the thread
}
int main() {
std::thread my_thread(my_function); // create a thread that runs my_function
my_thread.join(); // wait for my_thread to finish
return 0;
}
Threads are stopped when they finish executing their code, but they can also be stopped prematurely using std::thread::detach
.
You can control threads in several ways. The most common way is to use std::this_thread::sleep_for
to make a thread sleep for a certain period of time.
Here are some practical examples of working with threads in C++.
#include <iostream>
#include <thread>
// This function will be executed by the thread
void thread_function() {
std::cout << "Hello from thread!\n";
}
int main() {
// Create a thread that runs thread_function
std::thread my_thread(thread_function);
// Wait for my_thread to finish
my_thread.join();
std::cout << "Hello from main!\n";
return 0;
}
Expected output:
Hello from thread!
Hello from main!
#include <iostream>
#include <thread>
#include <chrono>
void thread_function() {
std::this_thread::sleep_for(std::chrono::seconds(3)); // make the thread sleep for 3 seconds
std::cout << "Hello from thread after 3 seconds!\n";
}
int main() {
std::thread my_thread(thread_function);
my_thread.join();
std::cout << "Hello from main!\n";
return 0;
}
Expected output (after 3 seconds):
Hello from thread after 3 seconds!
Hello from main!
In this tutorial, we have covered:
- How to create threads in C++
- How to stop and control threads
As a next step, you can learn about more advanced threading concepts, such as mutexes and condition variables. The C++ documentation is a great resource for that.
Here are some exercises for you to practice:
Solutions:
#include <iostream>
#include <thread>
void thread_function1() {
std::cout << "Hello from thread 1!\n";
}
void thread_function2() {
std::cout << "Hello from thread 2!\n";
}
int main() {
std::thread my_thread1(thread_function1);
std::thread my_thread2(thread_function2);
my_thread1.join();
my_thread2.join();
return 0;
}
2.
#include <iostream>
#include <thread>
#include <chrono>
void thread_function() {
std::this_thread::sleep_for(std::chrono::seconds(5)); // make the thread sleep for 5 seconds
std::cout << "Hello from thread after 5 seconds!\n";
}
int main() {
std::thread my_thread(thread_function);
std::cout << "Hello from main!\n";
my_thread.join();
return 0;
}
Remember, practice is key. Keep experimenting with these concepts to gain a deeper understanding of threads in C++.