Threads
What is a Thread?
Why Use Threads?
Threads in C++17
Basic Usage of Threads in C++17
#include <iostream>
#include <thread>
// A simple function that will be executed by a thread
void print_message(const std::string& message) {
std::cout << message << std::endl;
}
int main() {
// Creating a thread that runs the print_message function
std::thread t1(print_message, "Hello from Thread 1!");
// Lambda function to be executed by a second thread
std::thread t2([]() {
std::cout << "Hello from Thread 2!" << std::endl;
});
// Wait for both threads to finish execution
t1.join();
t2.join();
std::cout << "Main thread finished!" << std::endl;
return 0;
}Key Points about Threads in C++17
Conclusion
Last updated