> For the complete documentation index, see [llms.txt](https://notes.tejpratapsingh.com/_/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.tejpratapsingh.com/_/cpp/thread/threading.md).

# Threading

Threading in C++ allows for concurrent execution of different parts of a program, which can improve performance and responsiveness, especially in multi-core systems. Here are some basic concepts of threading in C++:

1. Thread creation: You can create threads using the `std::thread` class from the `<thread>` header.
2. Thread functions: These are the functions that run in separate threads.
3. Joining threads: The `join()` method waits for a thread to complete its execution.
4. Detaching threads: The `detach()` method allows a thread to run independently.
5. Mutex (Mutual Exclusion): Used to protect shared resources from concurrent access.
6. Lock guards: RAII-style wrappers for mutexes to ensure proper locking and unlocking.
7. Condition variables: Used for thread synchronization and communication.
8. Atomic operations: Provide thread-safe access to shared variables without explicit locking.
