C++编程思想 第2卷 第11章 并发 使用线程 使用执行器简化工作

本文介绍了C++中使用ZThread库的Executor如何减少并发编程的复杂性,通过Executor对象实现任务与执行之间的间接层,避免直接创建和管理线程。提到了ThreadedExecutor可能带来的过多开销,并推荐使用PoolExecutor进行线程池管理,以提高效率并控制资源消耗。同时,还讨论了ConcurrentExecutor和SynchronousExecutor在不同场景的应用。
摘要由CSDN通过智能技术生成

使用ZThread的执行器 Executor 可以减少编码的工作量
执行器在客户和任务的执行之间提供了一个间接层
客户不再直接执行任务
而是由一个中间的对象来执行该任务这里要改个名字 要不然会重名
出现函数未定义

//: c11:ThreadedExecutor1.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
//{L} ZThread
#include <iostream>
#include "zthread/ThreadedExecutor.h"
#include "LiftOff.h"
using namespace ZThread;
using namespace std;

int main() {
  try {
    ThreadedExecutor executor;
    for(int i = 0; i < 5; i++)
      executor.execute(new LiftOff(10, i));
  } catch(Synchronization_Exception& e) {
    cerr << e.what() << endl;
  }
  getchar();
} ///:~


输出
ThreadQueue created
User thread created.
Reference thread created.
1 reference-thread added.
pollPendingThreads()
1 user-thread added.
Thread starting...
User thread created.
0pollPendingThreads()
:1 user-thread added.
9Thread starting...
User thread created.
1
:90
:18:
8pollPendingThreads()

1 user-thread added.
10User thread created.
::77

10Thread starting...
:6
1pollPendingThreads()
1 user-thread added.
2Thread starting...
:36:
9:
5User thread created.

0:2pollPendingThreads()
1 user-thread added.
5
0::49Thread starting...

0::348

3:18
:4:
9

3:27:
7
1:03
4::83
:6
4:17:22
:6

2:25

42:4
1::106

4:5
2:1:03::51
4:
43

0:
0
Liftoff!
4:23
3:44:
Liftoff!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值