欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > windows C++-并行编程-创建在延迟一段时间后完成的任务

windows C++-并行编程-创建在延迟一段时间后完成的任务

2025/5/2 15:54:47 来源:https://blog.csdn.net/m0_72813396/article/details/141578980  浏览:    关键词:windows C++-并行编程-创建在延迟一段时间后完成的任务

此示例演示如何使用 concurrency::task、concurrency::cancellation_token_source、concurrency::cancellation_token、concurrency::task_completion_event、concurrency::timer 和 concurrency::call 类创建在延迟一段时间后完成的任务。 可以使用此方法生成偶尔轮询数据的循环。 还可介绍超时、未在预定时间内处理用户输入等情况。

示例:complete_after 和 cancel_after_timeout 函数

下面的示例显示 complete_after 和 cancel_after_timeout 函数。 complete_after 函数将创建在指定的延迟后完成的 task 对象。 它使用 timer 对象和 call 对象在指定延迟后设置 task_completion_event 对象。 通过使用 task_completion_event 类,您可以定义在一个线程或另一个任务发出有可用值的信号后完成的任务。 设置事件后,侦听器任务完成,它们的延续按计划运行。

如果某任务未在给定超时之前完成,则 cancel_after_timeout 函数基于 complete_after 函数构建以取消该任务。 cancel_after_timeout 函数将创建两个任务。 第一个任务指示成功,并在提供的任务完成后完成。 第二个任务指示失败并在指定的超时后完成。 cancel_after_timeout 函数将创建一个在成功或失败任务完成后运行的延续任务。 如果失败任务先完成,则延续将取消标记源,以便取消整个任务。

// Creates a task that completes after the specified delay.
task<void> complete_after(unsigned int timeout)
{// A task completion event that is set when a timer fires.task_completion_event<void> tce;// Create a non-repeating timer.auto fire_once = new timer<int>(timeout, 0, nullptr, false);// Create a call object that sets the completion event after the timer fires.auto callback = new call<int>([tce](int){tce.set();});// Connect the timer to the callback and start the timer.fire_once->link_target(callback);fire_once->start();// Create a task that completes after the completion event is set.task<void> event_set(tce);// Create a continuation task that cleans up resources and// and return that continuation task.return event_set.then([callback, fire_once](){delete callback;delete fire_once;});
}// Cancels the provided task after the specifed delay, if the task
// did not complete.
template<typename T>
task<T> cancel_after_timeout(task<T> t, cancellation_token_source cts, unsigned int timeout)
{// Create a task that returns true after the specified task completes.task<bool> success_task = t.then([](T){return true;});// Create a task that returns false after the specified timeout.task<bool> failure_task = complete_after(timeout).then([]{return false;});// Create a continuation task that cancels the overall task // if the timeout task finishes first.return (failure_task || success_task).then([t, cts](bool success){if(!success){// Set the cancellation token. The task that is passed as the// t parameter should respond to the cancellation and stop// as soon as it can.cts.cancel();}// Return the original task.return t;});
}
示例:计算质数计数

下面的示例多次计算范围 [0, 100000] 内质数的计数。 如果未在给定时间限制内完成,则操作失败。 count_primes 函数演示如何使用 cancel_after_timeout 函数。 它会对给定范围内的质数进行计数,而且如果任务未在提供的时间内完成,则会失败。 wmain 函数多次调用 count_primes 函数。 每次将时间限制减半。 当操作未在当前时间限制内完成后,程序结束。

// Determines whether the input value is prime.
bool is_prime(int n)
{if (n < 2)return false;for (int i = 2; i < n; ++i){if ((n % i) == 0)return false;}return true;
}// Counts the number of primes in the range [0, max_value].
// The operation fails if it exceeds the specified timeout.
bool count_primes(unsigned int max_value, unsigned int timeout)
{cancellation_token_source cts;// 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, cts]{combinable<size_t> counts;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.if (cts.get_token().is_canceled()){cancel_current_task();}// NOTE: You can replace the calls to is_canceled// and cancel_current_task with a call to interruption_point.// interruption_point();// Increment the local counter if the value is prime.if (is_prime(n)){counts.local()++;}});// Return the sum of counts across all threads.return counts.combine(plus<size_t>());}, cts.get_token()), cts, timeout);// Print the result.try{auto primes = t.get();wcout << L"Found " << primes << L" prime numbers within " << timeout << L" ms." << endl;return true;}catch (const task_canceled&){wcout << L"The task timed out." << endl;return false;}
}int wmain()
{// Compute the count of prime numbers in the range [0, 100000] // until the operation fails.// Each time the test succeeds, the time limit is halved.unsigned int max = 100000;unsigned int timeout = 5000;bool success = true;do{success = count_primes(max, timeout);timeout /= 2;} while (success);
}
/* Sample output:Found 9592 prime numbers within 5000 ms.Found 9592 prime numbers within 2500 ms.Found 9592 prime numbers within 1250 ms.Found 9592 prime numbers within 625 ms.The task timed out.
*/

当使用此技术在延迟后取消任务时,任何尚未启动的任务在整个任务取消后都不会启动。 但是,快速响应取消对任何长时间运行的任务而言都非常重要。 

完整代码示例

以下是该示例的完整代码:

// task-delay.cpp
// compile with: /EHsc
#include <ppl.h>
#include <ppltasks.h>
#include <agents.h>
#include <iostream>using namespace concurrency;
using namespace std;// Creates a task that completes after the specified delay.
task<void> complete_after(unsigned int timeout)
{// A task completion event that is set when a timer fires.task_completion_event<void> tce;// Create a non-repeating timer.auto fire_once = new timer<int>(timeout, 0, nullptr, false);// Create a call object that sets the completion event after the timer fires.auto callback = new call<int>([tce](int){tce.set();});// Connect the timer to the callback and start the timer.fire_once->link_target(callback);fire_once->start();// Create a task that completes after the completion event is set.task<void> event_set(tce);// Create a continuation task that cleans up resources and// and return that continuation task.return event_set.then([callback, fire_once](){delete callback;delete fire_once;});
}// Cancels the provided task after the specifed delay, if the task
// did not complete.
template<typename T>
task<T> cancel_after_timeout(task<T> t, cancellation_token_source cts, unsigned int timeout)
{// Create a task that returns true after the specified task completes.task<bool> success_task = t.then([](T){return true;});// Create a task that returns false after the specified timeout.task<bool> failure_task = complete_after(timeout).then([]{return false;});// Create a continuation task that cancels the overall task // if the timeout task finishes first.return (failure_task || success_task).then([t, cts](bool success){if(!success){// Set the cancellation token. The task that is passed as the// t parameter should respond to the cancellation and stop// as soon as it can.cts.cancel();}// Return the original task.return t;});
}// Determines whether the input value is prime.
bool is_prime(int n)
{if (n < 2)return false;for (int i = 2; i < n; ++i){if ((n % i) == 0)return false;}return true;
}// Counts the number of primes in the range [0, max_value].
// The operation fails if it exceeds the specified timeout.
bool count_primes(unsigned int max_value, unsigned int timeout)
{cancellation_token_source cts;// 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, cts]{combinable<size_t> counts;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.if (cts.get_token().is_canceled()){cancel_current_task();}// NOTE: You can replace the calls to is_canceled// and cancel_current_task with a call to interruption_point.// interruption_point();// Increment the local counter if the value is prime.if (is_prime(n)){counts.local()++;}});// Return the sum of counts across all threads.return counts.combine(plus<size_t>());}, cts.get_token()), cts, timeout);// Print the result.try{auto primes = t.get();wcout << L"Found " << primes << L" prime numbers within " << timeout << L" ms." << endl;return true;}catch (const task_canceled&){wcout << L"The task timed out." << endl;return false;}
}int wmain()
{// Compute the count of prime numbers in the range [0, 100000] // until the operation fails.// Each time the test succeeds, the time limit is halved.unsigned int max = 100000;unsigned int timeout = 5000;bool success = true;do{success = count_primes(max, timeout);timeout /= 2;} while (success);
}
/* Sample output:Found 9592 prime numbers within 5000 ms.Found 9592 prime numbers within 2500 ms.Found 9592 prime numbers within 1250 ms.Found 9592 prime numbers within 625 ms.The task timed out.
*/
 编译代码

若要编译代码,请复制代码并将其粘贴到 Visual Studio 项目中,或粘贴到一个名为 task-delay.cpp 的文件中,然后在 Visual Studio 命令提示符窗口中运行以下命令。

cl.exe /EHsc task-delay.cpp

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词