Multithreading uses multiple threads of execution within one process. It is one way to make an application concurrent; another is to run separate processes that communicate through channels such as sockets or pipes. Threads are useful when related work needs to proceed independently, but their shared resources make coordination part of the design. section 1.1.2 section 8.0
Start one thread and wait for it
#include <iostream> #include <thread> void hello() { std::cout << "Hello Concurrent World\n"; } int main() { std::thread t(hello); t.join(); } Execution begins in main(). Constructing t starts another thread whose initial function is hello(), while the initial thread continues. Calling join() makes the initial thread wait for t to finish. Without that wait, main() could reach its end before the message is printed. This example demonstrates thread creation, not a worthwhile performance optimization: the initial thread has no other work to do. section 1.4.1
A stronger reason to use separate threads is separation of concerns. A media application can handle user input on one thread while another reads and plays media. A Pause request still needs to pass between them, but playback code no longer has to check repeatedly for input. This division can improve responsiveness even when the thread count is chosen for the application’s structure rather than the number of CPU cores. section 1.2.1
Before dividing work among threads, decide:
- Which work belongs on each thread, and which threads must wait for others. section 8.0
- Which data will be shared and how access to it will be synchronized. section 8.0
- Whether the benefit justifies the complexity and resource cost. Starting threads takes time; too many threads consume stacks and operating-system resources. section 1.2.3
In C++, portable standard-library support for multithreading begins with C++11. Earlier C++ programs relied on platform-specific extensions and APIs or libraries built around them. The mechanics of launching a thread are relatively small; the harder task is ensuring that the resulting program behaves as intended. section 1.3.1 section 1.5










