Skip to content

Repo sync for protected CLA branch #2545

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

Merged
merged 14 commits into from
Oct 20, 2020
Merged
13 changes: 8 additions & 5 deletions docs/c-language/noreturn.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
---
title: "_Noreturn keyword and noreturn macro (C11)"
description: "Describes the `_Noreturn` keyword and `noreturn` macro."
ms.date: 10/16/2020
ms.date: 10/19/2020
f1_keywords: ["_Noreturn_c", "noreturn"]
helpviewer_keywords: ["keywords [C]"]
---

# `_Noreturn` keyword and `noreturn` macro (C11)

The `_Noreturn` keyword was introduced in C11. It tells the compiler that the function it's applied to doesn't return. The compiler knows that the code following a call to a `_Noreturn` function is unreachable.
The `_Noreturn` keyword was introduced in C11. It tells the compiler that the function it's applied to doesn't return to the caller. The compiler knows that the code following a call to a `_Noreturn` function is unreachable. An example of a function that doesn't return is [abort](../c-runtime-library/reference/abort.md). If there's a possibility for control flow to return to the caller, the function must not have the `_Noreturn` attribute.

A convenience macro, `noreturn`, provided in <stdnoreturn.h>, maps to the `_Noreturn` keyword.
The keyword is typically used through the convenience macro, `noreturn`, provided in <stdnoreturn.h>, which maps to the `_Noreturn` keyword.

The primary benefits for using `_Noreturn` (or the equivalent `noreturn`) are making the intention of the function clear in the code for future readers, and detecting unintentionally unreachable code.

A function marked `noreturn` shouldn't include a return type because it doesn't return a value to the caller. It should be `void`.

## Example using `noreturn` macro and `_Noreturn `keyword

The following example demonstrates the `_Noreturn` keyword and the equivalent `noreturn` macro.
Expand Down Expand Up @@ -61,5 +63,6 @@ int main(void)
## See also

[/std (Specify language standard version)](../build/reference/std-specify-language-standard-version.md)\
[/W4 (Specify warning level)](../build/reference/compiler-option-warning-level.md)
[C4702 warning](../error-messages\compiler-warnings\compiler-warning-level-4-c4702.md)
[/W4 (Specify warning level)](../build/reference/compiler-option-warning-level.md)\
[C4702 warning](../error-messages\compiler-warnings\compiler-warning-level-4-c4702.md)\
[__declspec(noreturn)](../cpp/noreturn.md)
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ bool count_primes(unsigned int max_value, unsigned int timeout)

// Create a task that computes the count of prime numbers.
// The task is canceled after the specified timeout.
auto t = cancel_after_timeout(task<size_t>([max_value, timeout]
auto t = cancel_after_timeout(task<size_t>([max_value, timeout, cts]
{
combinable<size_t> counts;
parallel_for<unsigned int>(0, max_value + 1, [&counts](unsigned int n)
parallel_for<unsigned int>(0, max_value + 1, [&counts, cts](unsigned int n)
{
// Respond if the overall task is cancelled by canceling
// the current task.
Expand Down Expand Up @@ -52,7 +52,7 @@ bool count_primes(unsigned int max_value, unsigned int timeout)
<< timeout << L" ms." << endl;
return true;
}
catch (const task_canceled& e)
catch (const task_canceled&)
{
wcout << L"The task timed out." << endl;
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// task-delay.cpp
// compile with: /EHsc
#include <ppl.h>
#include <ppltasks.h>
#include <agents.h>
#include <iostream>
Expand Down Expand Up @@ -91,10 +92,10 @@ bool count_primes(unsigned int max_value, unsigned int timeout)

// Create a task that computes the count of prime numbers.
// The task is canceled after the specified timeout.
auto t = cancel_after_timeout(task<size_t>([max_value, timeout]
auto t = cancel_after_timeout(task<size_t>([max_value, timeout, cts]
{
combinable<size_t> counts;
parallel_for<unsigned int>(0, max_value + 1, [&counts](unsigned int n)
parallel_for<unsigned int>(0, max_value + 1, [&counts, cts](unsigned int n)
{
// Respond if the overall task is cancelled by canceling
// the current task.
Expand Down Expand Up @@ -124,7 +125,7 @@ bool count_primes(unsigned int max_value, unsigned int timeout)
<< timeout << L" ms." << endl;
return true;
}
catch (const task_canceled& e)
catch (const task_canceled&)
{
wcout << L"The task timed out." << endl;
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
title: "How to: Create a Task that Completes After a Delay"
ms.date: "11/04/2016"
title: "How to: Create a task that completes after a delay"
description: "Create a task that completes after a delay by using the PPL ConcRT library."
ms.date: 10/19/2020
helpviewer_keywords: ["task_completion_event class, example", "create a task that completes after a delay, example [C++]"]
ms.assetid: 3fc0a194-3fdb-4eba-8b8a-b890981a985d
---
# How to: Create a Task that Completes After a Delay
# How to: Create a task that completes after a delay

This example shows how to use the [concurrency::task](../../parallel/concrt/reference/task-class.md), [concurrency::cancellation_token_source](../../parallel/concrt/reference/cancellation-token-source-class.md), [concurrency::cancellation_token](../../parallel/concrt/reference/cancellation-token-class.md), [concurrency::task_completion_event](../../parallel/concrt/reference/task-completion-event-class.md), [concurrency::timer](../../parallel/concrt/reference/timer-class.md), and [concurrency::call](../../parallel/concrt/reference/call-class.md) classes to create a task that completes after a delay. You can use this method to build loops that occasionally poll for data, introduce timeouts, delay handling of user input for a predetermined time, and so on.
This example shows how to use the [`concurrency::task`](../../parallel/concrt/reference/task-class.md), [`concurrency::cancellation_token_source`](../../parallel/concrt/reference/cancellation-token-source-class.md), [`concurrency::cancellation_token`](../../parallel/concrt/reference/cancellation-token-class.md), [`concurrency::task_completion_event`](../../parallel/concrt/reference/task-completion-event-class.md), [`concurrency::timer`](../../parallel/concrt/reference/timer-class.md), and [`concurrency::call`](../../parallel/concrt/reference/call-class.md) classes to create a task that completes after a delay. You can use this method to build loops that occasionally poll for data. You can also introduce timeouts, delay handling of user input for a predetermined time, and so on.

## Example: complete_after and cancel_after_timeout functions

Expand All @@ -15,17 +16,17 @@ The following example shows the `complete_after` and `cancel_after_timeout` func
> [!TIP]
> For more information about the `timer` and `call` classes, which are part of the Asynchronous Agents Library, see [Asynchronous Message Blocks](../../parallel/concrt/asynchronous-message-blocks.md).

The `cancel_after_timeout` function builds on the `complete_after` function to cancel a task if that task does not complete before a given timeout. The `cancel_after_timeout` function creates two tasks. The first task indicates success and completes after the provided task completes; the second task indicates failure and completes after the specified timeout. The `cancel_after_timeout` function creates a continuation task that runs when the success or failure task completes. If the failure task completes first, the continuation cancels the token source to cancel the overall task.
The `cancel_after_timeout` function builds on the `complete_after` function to cancel a task if that task doesn't complete before a given timeout. The `cancel_after_timeout` function creates two tasks. The first task indicates success and completes after the provided task completes. The second task indicates failure and completes after the specified timeout. The `cancel_after_timeout` function creates a continuation task that runs when the success or failure task completes. If the failure task completes first, the continuation cancels the token source to cancel the overall task.

[!code-cpp[concrt-task-delay#1](../../parallel/concrt/codesnippet/cpp/how-to-create-a-task-that-completes-after-a-delay_1.cpp)]

## Example: Compute count of prime numbers

The following example computes the count of prime numbers in the range [0, 100000] multiple times. The operation fails if it does not complete in a given time limit. The `count_primes` function demonstrates how to use the `cancel_after_timeout` function. It counts the number of primes in the given range and fails if the task does not complete in the provided time. The `wmain` function calls the `count_primes` function multiple times. Each time, it halves the time limit. The program finishes after the operation does not complete in the current time limit.
The following example computes the count of prime numbers in the range [0, 100000] multiple times. The operation fails if it doesn't complete in a given time limit. The `count_primes` function demonstrates how to use the `cancel_after_timeout` function. It counts the number of primes in the given range and fails if the task doesn't complete in the provided time. The `wmain` function calls the `count_primes` function multiple times. Each time, it halves the time limit. The program finishes after the operation doesn't complete in the current time limit.

[!code-cpp[concrt-task-delay#2](../../parallel/concrt/codesnippet/cpp/how-to-create-a-task-that-completes-after-a-delay_2.cpp)]

When you use this technique to cancel tasks after a delay, any unstarted tasks will not start after the overall task is canceled. However, it is important for any long-running tasks to respond to cancellation in a timely manner. For more information about task cancellation, see [Cancellation in the PPL](cancellation-in-the-ppl.md).
When you use this technique to cancel tasks after a delay, any unstarted tasks won't start after the overall task is canceled. However, it's important for any long-running tasks to respond to cancellation quickly. For more information about task cancellation, see [Cancellation in the PPL](cancellation-in-the-ppl.md).

## Complete code example

Expand Down