- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <thread>
Introduction
Thread is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address spac.
Member types
| Sr.No. | Member type & description |
|---|---|
| 1 |
id
It is a thread id. |
| 2 |
Native handle type
It is a native handle type. |
Member functions
| Sr.No. | Member function & description |
|---|---|
| 1 |
(constructor)
It is used to construct thread. |
| 2 |
(destructor)
It is used to destructor thread. |
| 3 |
operator=
It is a move-assign thread. |
| 4 |
get_id
It is used to get thread id. |
| 5 |
joinable
It is used to check if joinable. |
| 6 |
join
It is used to join thread. |
| 7 |
detach
It is used to detach thread. |
| 8 |
swap
It is used to swap threads. |
| 9 |
native_handle
It is used to get native handle. |
| 10 |
hardware_concurrency [static]
It is used to detect hardware concurrency. |
Non-member overloads
| Sr.No. | Non-member overload & description |
|---|---|
| 1 |
swap (thread)
It is used to swap threads. |
Example
In below example for std::thread.
#include <iostream>
#include <thread>
void foo() {
std::cout << " foo is executing concurrently...\n";
}
void bar(int x) {
std::cout << " bar is executing concurrently...\n";
}
int main() {
std::thread first (foo);
std::thread second (bar,0);
std::cout << "main, foo and bar now execute concurrently...\n";
first.join();
second.join();
std::cout << "foo and bar completed.\n";
return 0;
}
The output should be like this −
main, foo and bar now execute concurrently... bar is executing concurrently... foo is executing concurrently... foo and bar completed.
Advertisements