Skip to content

Improve <thread> functions reference #5599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions docs/standard-library/thread-functions.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
description: "Learn more about: <thread> functions"
title: "<thread> functions"
ms.date: "11/04/2016"
description: "Learn more about: <thread> functions"
ms.date: 11/04/2016
f1_keywords: ["thread/std::get_id", "thread/std::sleep_for", "thread/std::sleep_until", "thread/std::swap", "thread/std::yield"]
ms.assetid: bb1aa1ef-fe3f-4e2c-8b6e-e22dbf2f5a19
helpviewer_keywords: ["std::get_id [C++]", "std::sleep_for [C++]", "std::sleep_until [C++]", "std::swap [C++]", "std::yield [C++]"]
---
# `<thread>` functions

## <a name="get_id"></a> get_id
The `<thread>` header provides the following functions:

## <a name="get_id"></a> `get_id`

Uniquely identifies the current thread of execution.

Expand All @@ -18,48 +19,62 @@ thread::id this_thread::get_id() noexcept;

### Return Value

An object of type [thread::id](../standard-library/thread-class.md) that uniquely identifies the current thread of execution.
An object of type [`thread::id`](thread-class.md#id_class) that uniquely identifies the current thread of execution.

### Example

```cpp
#include <iostream>
#include <thread>

int main()
{
std::thread::id current_thread_id = std::this_thread::get_id();
std::cout << "Current thread id: " << current_thread_id;
}
```

## <a name="sleep_for"></a> sleep_for
```Output
Current thread id: 16196
```

## <a name="sleep_for"></a> `sleep_for`

Blocks the calling thread.

```cpp
template <class Rep,
class Period>
inline void sleep_for(const chrono::duration<Rep, Period>& Rel_time);
template <class Rep, class Period>
void this_thread::sleep_for(const chrono::duration<Rep, Period>& Rel_time);
```

### Parameters

*Rel_time*\
A [duration](../standard-library/duration-class.md) object that specifies a time interval.
*`Rel_time`*\
A [`duration`](duration-class.md) object that specifies a time interval.

### Remarks

The function blocks the calling thread for at least the time that's specified by *Rel_time*. This function does not throw any exceptions.
The function blocks the calling thread for at least the time that's specified by *`Rel_time`*. This function does not throw any exceptions.

## <a name="sleep_until"></a> sleep_until
## <a name="sleep_until"></a> `sleep_until`

Blocks the calling thread at least until the specified time.

```cpp
template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>& Abs_time);

void sleep_until(const xtime *Abs_time);
void this_thread::sleep_until(const chrono::time_point<Clock, Duration>& Abs_time);
```

### Parameters

*Abs_time*\
*`Abs_time`*\
Represents a point in time.

### Remarks

This function does not throw any exceptions.

## <a name="swap"></a> swap
## <a name="swap"></a> `swap`

Swaps the states of two `thread` objects.

Expand All @@ -69,24 +84,24 @@ void swap(thread& Left, thread& Right) noexcept;

### Parameters

*Left*\
*`Left`*\
The left `thread` object.

*Right*\
*`Right`*\
The right `thread` object.

### Remarks

The function calls `Left.swap(Right)`.

## <a name="yield"></a> yield
## <a name="yield"></a> `yield`

Signals the operating system to run other threads, even if the current thread would ordinarily continue to run.

```cpp
inline void yield() noexcept;
inline void this_thread::yield() noexcept;
```

## See also

[\<thread>](../standard-library/thread.md)
[`<thread>`](thread.md)