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

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

使用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!
:2Thread joining...

4:1Thread exiting...

4:insertPendingThread()
0
Thread joining...
Thread exiting...
insertPendingThread()
Liftoff!
3:231 pending-thread added.

3:20Thread joining...
 completed

3:
1
1 pending-thread added.
Thread exiting...
insertPendingThread()
3:021 pending-thread added.

1 completed4
 completed
:1Liftoff!

2Thread joining...
Thread exiting...
:insertPendingThread()
01 pending-thread added.

3 completedLiftoff!

Thread joining...
Thread exiting...
insertPendingThread()
1 pending-thread added.
2 completed

某些情况下可以用单个的Executor对象来创建和管理系统中的所有线程
必须把线程处理代码放在一个try块中
因为如果出现错误的话Executor的execute()函数可能会抛出Synchronization_Exception异常

ThreadedExecutor 在产生的代码中
由于创建了太多的线程
ThreadedExecutor将会导致过多的开销
可以使用PoolExecutor对象来替换ThreadedExecutor对象
它使用一个有限的线程集以并行的方式提交任务

ZThread库有一个 PoolExecutor文件 

//: C11:PoolExecutor1.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/PoolExecutor.h"
#include "LiftOff.h"
using namespace ZThread;
using namespace std;

int main() {
  try {
    // Constructor argument is minimum number of threads:
    PoolExecutor executor(5);
    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.
pollPendingThreads()
1 user-thread added.
Thread starting...
User thread created.
pollPendingThreads()
1 user-thread added.
Thread starting...
User thread created.
pollPendingThreads()
1 user-thread added.
Thread starting...
User thread created.
pollPendingThreads()
1 user-thread added.
Thread starting...
041::993

4::829

:19:
:28:
931
:74
:7:
8
8
2:74
1::66
3
:70
:8
2:6
1:253
4::550:
1:42:
:4

7
0:612
6
:3
4:40
:3:
5
0:34:25:4
:32


2:1
0:342
:0:
23:40
:1:
2Liftoff!
3:
3
2
2 completed
4The task has thrown an unhandled exception
:1
3:Thread joining...
2
3:1Thread exiting...

3:10:
4:insertPendingThread()
0
1
1:1 pending-thread added.
0
Liftoff!
0:1Liftoff!
0:
0
3 completed
4 completed
Liftoff!Liftoff!

The task has thrown an unhandled exception
1Thread joining...
0Thread exiting...
 completed completed
insertPendingThread()
1 pending-thread added.
The task has thrown an unhandled exception
Thread joining...

The task has thrown an unhandled exception
Thread exiting...
Thread joining...
insertPendingThread()
Thread exiting...
1 pending-thread added.
insertPendingThread()
The task has thrown an unhandled exception
1 pending-thread added.
Thread joining...
Thread exiting...
insertPendingThread()
1 pending-thread added.

使用PoolExecutor
可以预先将开销很大的线程分配工作一次做完
在可能的时候重用这些线程
这样做节省时间
因为不会因不断地为了每个任务都创建一个线程而付出那些开销每个任务按其被提交的顺序执行
并且在下一个任务开始之前执行完成
一个ConcurrentExecutor对象串行化 顺序执行 提交给它的任务

//: C11:ConcurrentExecutor.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/ConcurrentExecutor.h"
#include "LiftOff.h"
using namespace ZThread;
using namespace std;

int main() {
  try {
    ConcurrentExecutor 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...
0:9
0:8
0:7
0:6
0:5
0:4
0:3
0:2
0:1
0:0
Liftoff!
0 completed
1:9
1:8
1:7
1:6
1:5
1:4
1:3
1:2
1:1
1:0
Liftoff!
1 completed
2:9
2:8
2:7
2:6
2:5
2:4
2:3
2:2
2:1
2:0
Liftoff!
2 completed
3:9
3:8
3:7
3:6
3:5
3:4
3:3
3:2
3:1
3:0
Liftoff!
3 completed
4:9
4:8
4:7
4:6
4:5
4:4
4:3
4:2
4:1
4:0
Liftoff!
4 completed
The task has thrown an unhandled exception
Thread joining...
Thread exiting...
insertPendingThread()
1 pending-thread added.

就像ConcurrentExecutor
SynchronousExecutor用于需要同一时刻只运行一个任务的时候
串行代替了并发
不像ConcurrentExecutor SynchronousExecutor自己不创建或管理线程


对资源的访问采用同步方式进行
SynchronousExecutor可以跳过对适当合理的某些原型事件进行协调的麻烦

//: C11:SynchronousExecutor.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/SynchronousExecutor.h"
#include "LiftOff.h"
using namespace ZThread;
using namespace std;

int main() {
  try {
    SynchronousExecutor 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
Reference thread created.
1 reference-thread added.
0:9
0:8
0:7
0:6
0:5
0:4
0:3
0:2
0:1
0:0
Liftoff!
0 completed
1:9
1:8
1:7
1:6
1:5
1:4
1:3
1:2
1:1
1:0
Liftoff!
1 completed
2:9
2:8
2:7
2:6
2:5
2:4
2:3
2:2
2:1
2:0
Liftoff!
2 completed
3:9
3:8
3:7
3:6
3:5
3:4
3:3
3:2
3:1
3:0
Liftoff!
3 completed
4:9
4:8
4:7
4:6
4:5
4:4
4:3
4:2
4:1
4:0
Liftoff!
4 completed

当运行程序时
将会看到任务以被提交的顺序执行
每个任务在下一个任务启动前完成
但是看不到有新线程创建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值