A thread is a path of execution within a process. Every C++ program begins with a thread running main(). It can launch additional threads, each with its own entry function. Those threads run concurrently with the initial thread and exit when their entry functions return. section 2.1
Threads can separate work that needs to remain responsive from work that takes time. A DVD player, for example, can handle user input in one thread while another reads and plays the DVD. The threads still need to interact when the user presses Pause, but playback code need not repeatedly check for input. section 1.2.1
Listing 1.1. A simple Hello Concurrent World program
#include <iostream>
#include <thread>
void hello()
{
std::cout<<"Hello Concurrent World\n";
}
int main()
{
std::thread t(hello); 3
t.join();
}
In this small C++ program, std::thread t(hello) starts a second thread at hello(), while the initial thread continues in main(). The call to t.join() makes the initial thread wait; without it, main() could finish before the message is printed. section 1.4.1
Choose threads for a clear benefit, not merely because work can be divided:
- Threads in one process provide a different way to organize concurrent work than separate processes. Separate processes communicate through channels such as pipes or sockets; threads share a process and must account for shared resources. section 1.1.2
- Launching a thread takes time and requires operating-system resources and stack space. A short task may finish faster if the existing thread performs it directly. section 1.2.3
- Multiple threads also make code harder to reason about and maintain. Use them when responsiveness, separation of concerns, or a worthwhile performance gain justifies that cost. section 1.2.1 section 1.2.3











