Visual C++本地并行编程中的状态管理

2020-01-06 12:36:01王旭

任务组取消

  新的Beta1 PPL的其中一个功能就是可以取消正在运行中的任务组。在task_group类型上加入run 和 wait方法是一个新的cancel方法。还有一个相应的is_canceling方法让你可以检查在进程中是否完成取消。task_group_status列举也有一个新的值叫做canceled,让你检查取消是否发生。以下的代码展示了这些新的功能:

  //declare tasks and run tasks

  task_group tg;

  tg.run(([]{printf("consoleWrite0n");}));

  tg.run(([]{printf("consoleWrite1n");}));

  //cancel tasks   tg.cancel();

  //check whether tasks are being cancelled

  bool taskGroupIsCanceling = tg.is_canceling();

  //check on status of task group

  task_group_status status = tg.wait();

  if (status == completed){

  printf("Tasks were completed successfullyn");

  }