哪个网站可以免费做电子请柬,wordpress+下载受限,做网站克隆,做企业宣传网站公司并行模式库 (PPL) 中取消操作的角色、如何取消并行工作以及如何确定取消并行工作的时间。
运行时使用异常处理实现取消操作。 请勿在代码中捕捉或处理这些异常。 此外#xff0c;还建议你在任务的函数体中编写异常安全的代码。 例如#xff0c;可以使用获取资源即初始化 (RA…并行模式库 (PPL) 中取消操作的角色、如何取消并行工作以及如何确定取消并行工作的时间。
运行时使用异常处理实现取消操作。 请勿在代码中捕捉或处理这些异常。 此外还建议你在任务的函数体中编写异常安全的代码。 例如可以使用获取资源即初始化 (RAII) 模式以确保在任务体中引发异常时正确处理资源。
使用异常来取消并行工作
要取消并行工作树使用取消标记和 cancel 方法比使用异常处理更有效。 取消标记和 cancel 方法由上向下取消任务和所有子任务。 相反异常处理以自下而上的方式工作并且必须在异常向上传播时单独取消每个子任务组。 异常处理主题介绍了并发运行时如何使用异常来传递错误。 但是并非所有异常都表示错误。 例如搜索算法可能在找到结果时取消其关联的任务。 但是如上所述在取消并行工作时异常处理的效率比使用 cancel 方法低。
注意如非必要建议你不要使用异常来取消并行工作。 取消标记和任务组 cancel 方法更高效且更不易出错。
当在传递给任务组的工作函数体中引发异常时运行时存储该异常并将该异常封送到等待任务组完成的上下文。 与 cancel 方法一样运行时将放弃任何尚未启动的任务并且不接受新任务。
第三个示例与第二个示例类似只不过任务 t4 引发异常来取消任务组 tg2。 此示例使用 try-catch 块在任务组 tg2 等待其子任务完成时检查取消情况。 与第一个示例类似这会导致任务组 tg2 进入已取消状态但不会取消任务组 tg1。
structured_task_group tg2;// Create a child task.
auto t4 make_task([] {// Perform work in a loop.for (int i 0; i 1000; i){// Call a function to perform work.// If the work function fails, throw an exception to // cancel the parent task.bool succeeded work(i);if (!succeeded){throw exception(The task failed);}}
});// Create a child task.
auto t5 make_task([] {// TODO: Perform work here.
});// Run the child tasks.
tg2.run(t4);
tg2.run(t5);// Wait for the tasks to finish. The runtime marshals any exception
// that occurs to the call to wait.
try
{tg2.wait();
}
catch (const exception e)
{wcout e.what() endl;
}
第四个示例使用异常处理来取消整个工作树。 此示例在任务组 tg1 等待其子任务完成时而不是任务组 tg2 等待其子任务完成时捕获异常。 与第二个示例类似这会导致树中的两个任务组 tg1 和 tg2 都进入已取消状态。
// Run the child tasks.
tg1.run(t1);
tg1.run(t2);
tg1.run(t3); // Wait for the tasks to finish. The runtime marshals any exception
// that occurs to the call to wait.
try
{tg1.wait();
}
catch (const exception e)
{wcout e.what() endl;
}
因为 task_group::wait 和 structured_task_group::wait 方法在子任务引发异常时引发所以你没有从它们收到返回值。
取消并行算法
PPL 中的并行算法(如 parallel_for)基于任务组生成。 因此你可以使用许多相同的技术来取消并行算法。
以下示例说明了几种取消并行算法的方法。
下面的示例使用 run_with_cancellation_token 函数调用 parallel_for 算法。 run_with_cancellation_token 函数采用一个取消标记作为参数并同步调用提供的工作函数。 因为并行算法基于任务生成它们继承父任务的取消标记。 因此parallel_for 可以响应取消。
// cancel-parallel-for.cpp
// compile with: /EHsc
#include ppltasks.h
#include iostream
#include sstreamusing namespace concurrency;
using namespace std;int wmain()
{// Call parallel_for in the context of a cancellation token.cancellation_token_source cts;run_with_cancellation_token([cts]() {// Print values to the console in parallel.parallel_for(0, 20, [cts](int n){// For demonstration, cancel the overall operation // when n equals 11.if (n 11){cts.cancel();}// Otherwise, print the value.else{wstringstream ss;ss n endl;wcout ss.str();}});}, cts.get_token());
}
/* Sample output:151617100185
*/
下面的示例使用 concurrency::structured_task_group::run_and_wait 方法来调用 parallel_for 算法。 structured_task_group::run_and_wait 方法等待提供的任务完成。 structured_task_group 对象可让工作函数取消该任务。
// To enable cancelation, call parallel_for in a task group.
structured_task_group tg;task_group_status status tg.run_and_wait([] {parallel_for(0, 100, [](int i) {// Cancel the task when i is 50.if (i 50){tg.cancel();}else{// TODO: Perform work here.}});
});// Print the task group status.
wcout LThe task group status is: ;
switch (status)
{
case not_complete:wcout Lnot complete. endl;break;
case completed:wcout Lcompleted. endl;break;
case canceled:wcout Lcanceled. endl;break;
default:wcout Lunknown. endl;break;
}输出:
The task group status is: canceled.
下面的示例使用异常处理来取消 parallel_for 循环。 运行时将异常封送到调用上下文。
try
{parallel_for(0, 100, [](int i) {// Throw an exception to cancel the task when i is 50.if (i 50){throw i;}else{// TODO: Perform work here.}});
}
catch (int n)
{wcout LCaught n endl;
}输出:
Caught 50
下面的示例使用一个布尔型标志来协调 parallel_for 循环中的取消。 每个任务都运行因为此示例不使用 cancel 方法或异常处理来取消整个任务集。 因此这种技术的计算开销可能比取消机制大。
// Create a Boolean flag to coordinate cancelation.
bool canceled false;parallel_for(0, 100, [](int i) {// For illustration, set the flag to cancel the task when i is 50.if (i 50){canceled true;}// Perform work if the task is not canceled.if (!canceled){// TODO: Perform work here.}
});
每个取消方法都有其他方法所没有的优点。 请选择适合你的特定需求的方法。
何时不使用取消
当一组相关任务中的每个成员可以及时退出时使用取消是恰当的。 但是在某些情况下取消可能不适合你的应用程序。 例如由于任务取消是协作性的如果任何单个任务被阻止则无法取消整个任务集。 例如如果一个任务尚未开始但它取消阻止另一个活动任务则在任务组已取消时它将不能启动。 这会导致应用程序中发生死锁。 可能不适合使用取消的另一个示例是任务被取消但其子任务会执行重要操作如释放资源。 因为在取消父任务时整个任务集也会被取消所以将无法执行此操作。 文章转载自: http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.lprfk.cn.gov.cn.lprfk.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.pffx.cn.gov.cn.pffx.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.krlsz.cn.gov.cn.krlsz.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.srhqm.cn.gov.cn.srhqm.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.mymz.cn.gov.cn.mymz.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.dschz.cn.gov.cn.dschz.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.jbxd.cn.gov.cn.jbxd.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn