| description | Learn more about: thread Class | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| title | thread Class | ||||||||||
| ms.date | 06/20/2022 | ||||||||||
| f1_keywords |
|
||||||||||
| helpviewer_keywords |
|
||||||||||
| ms.custom | devdivchpfy22 |
Defines an object that's used to observe and manage a thread of execution within an application.
class thread;You can use a thread object to observe and manage a thread of execution within an application. A thread object that's created by using the default constructor isn't associated with any thread of execution. A thread object that's constructed by using a callable object creates a new thread of execution and calls the callable object in that thread. Thread objects can be moved but not copied, which is why a thread of execution can be associated with only one thread object.
Every thread of execution has a unique identifier of type thread::id. The function this_thread::get_id returns the identifier of the calling thread. The member function thread::get_id returns the identifier of the thread that's managed by a thread object. For a default-constructed thread object, the thread::get_id method returns an object that has a value that's the same for all default-constructed thread objects and different from the value that's returned by this_thread::get_id for any thread of execution that could be joined at the time of the call.
| Name | Description |
|---|---|
id |
Uniquely identifies the associated thread. |
| Name | Description |
|---|---|
thread |
Constructs a thread object. |
| Name | Description |
|---|---|
detach |
Detaches the associated thread from the thread object. |
get_id |
Returns the unique identifier of the associated thread. |
hardware_concurrency |
Static. Returns an estimate of the number of hardware thread contexts. |
join |
Blocks until the associated thread completes. |
joinable |
Specifies whether the associated thread is joinable. |
native_handle |
Returns the implementation-specific type that represents the thread handle. |
swap |
Swaps the object state with a specified thread object. |
| Name | Description |
|---|---|
thread::operator= |
Associates a thread with the current thread object. |
Header: <thread>
Namespace: std
Detaches the associated thread. The operating system becomes responsible for releasing thread resources on termination.
void detach();After a call to detach, subsequent calls to get_id return id.
If the thread that's associated with the calling object isn't joinable, the function throws a system_error that has an error code of invalid_argument.
If the thread that's associated with the calling object is invalid, the function throws a system_error that has an error code of no_such_process.
Returns a unique identifier for the associated thread.
id get_id() const noexcept;A id object that uniquely identifies the associated thread, or id() if no thread is associated with the object.
Static method that returns an estimate of the number of hardware thread contexts.
static unsigned int hardware_concurrency() noexcept;An estimate of the number of hardware thread contexts. If the value can't be computed or isn't well defined, this method returns 0.
hardware_concurrency is currently defined to return the number of logical processors, which corresponds to the number of hardware threads that can execute simultaneously. It takes into account the number of physical processors, the number of cores in each physical processor, and simultaneous multithreading on each single core.
However, on systems with more than 64 logical processors this number is capped by the number of logical processors in a single group; see Processor Groups.
Provides a unique identifier for each thread of execution in the process.
class thread::id {
id() noexcept;
};The default constructor creates an object that doesn't compare equal to the thread::id object for any existing thread.
All default-constructed thread::id objects compare equal.
Blocks until the thread of execution that's associated with the calling object completes.
void join();If the call succeeds, subsequent calls to get_id for the calling object return a default thread::id that doesn't compare equal to the thread::id of any existing thread; if the call doesn't succeed, the value that's returned by get_id is unchanged.
Specifies whether the associated thread is joinable.
bool joinable() const noexcept;true if the associated thread is joinable; otherwise, false.
A thread object is joinable if get_id() != id().
Returns the implementation-specific type that represents the thread handle. The thread handle can be used in implementation-specific ways.
native_handle_type native_handle();native_handle_type is defined as a Win32 HANDLE that's cast as void *.
Associates the thread of a specified object with the current object.
thread& operator=(thread&& Other) noexcept;Other
A thread object.
*this
The method calls detach if the calling object is joinable.
After the association is made, Other is set to a default-constructed state.
Swaps the object state with that of a specified thread object.
void swap(thread& Other) noexcept;Other
A thread object.
Constructs a thread object.
thread() noexcept;
template <class Fn, class... Args>
explicit thread(Fn&& F, Args&&... A);
thread(thread&& Other) noexcept;F
An application-defined function to be executed by the thread.
A
A list of arguments to be passed to F.
Other
An existing thread object.
The first constructor constructs an object that's not associated with a thread of execution. The value that's returned by a call to get_id for the constructed object is thread::id().
The second constructor constructs an object that's associated with a new thread of execution and executes the pseudo-function INVOKE that's defined in <functional>. If not enough resources are available to start a new thread, the function throws a system_error object that has an error code of resource_unavailable_try_again. If the call to F terminates with an uncaught exception, terminate is called.
The third constructor constructs an object that's associated with the thread that's associated with Other. Other is then set to a default-constructed state.