C++编程思想 第2卷 第11章 并发 使用线程

本文介绍如何使用Runnable对象配合Thread类实现多线程程序。通过示例代码展示如何创建线程并驱动多个任务同时运行,以及如何处理线程运行过程中可能出现的异常。

为了使用线程驱动Runnable对象
就要创建独立的Thread对象
并且把一个Runnable指针传递给Thread的构造函数

//: C11:BasicThreads.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.
// The most basic use of the Thread class.
//{L} ZThread
#include <iostream>
#include "LiftOff.h"
#include "zthread/Thread.h"
using namespace ZThread;
using namespace std;

int main() {
  try {
    Thread t(new LiftOff(10));
    cout << "Waiting for LiftOff" << endl;
  } catch(Synchronization_Exception& e) {
    cerr << e.what() << endl;
  }
  getchar();
} ///:~


需要ZThread库
弄这个 先要弄include目录 然后把src目录下的所有cpp文件加入到工程 编译 运行
输出
ThreadQueue created
User thread created.
Reference thread created.
1 reference-thread added.
pollPendingThreads()
1 user-thread added.
Thread starting...
Waiting for LiftOff0
:9
0:8
0:7
0:6
0:5
0:4
0:3
0:2
0:1
0:0
Liftoff!
Thread joining...
Thread exiting...
insertPendingThread()
1 pending-thread added.
0 completed

Synchronization_Exception是ZThread库的一部分
并且使所有ZThread异常的基类
如果在启动或正在使用线程的时候错误发生
将会抛出异常

现在可以很容易地添加更多的线程来驱动更多的任务
线程如何与其他的线程协调运行

//: C11:MoreBasicThreads.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.
// Adding more threads.
//{L} ZThread
#include <iostream>
#include "LiftOff.h"
#include "zthread/Thread.h"
using namespace ZThread;
using namespace std;

int main() {
  const int SZ = 5;
  try {
    for(int i = 0; i < SZ; i++)
      Thread t(new LiftOff(10, i));
    cout << "Waiting for LiftOff" << endl;
  } 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.
0:9pollPendingThreads()

1 user-thread added.
0Thread starting...
:User thread created.
18:
90
:17:
08
:6
01::7
5
10:6
:pollPendingThreads()
141 user-thread added.

Thread starting...
:User thread created.
25:
91
:42
0:1::33
8

1:pollPendingThreads()
2
1:21:
1 user-thread added.
Thread starting...
3:User thread created.
9
7
2:6
0:12:pollPendingThreads()

0:013

0:1 user-thread added.
0
Liftoff!
Thread starting...
4:9Liftoff!
:
Thread joining...
Thread exiting...
insertPendingThread()
Thread joining...
Waiting for LiftOff
2:45:1 pending-thread added.
8
Thread exiting...
insertPendingThread()

8
1 completed
1 pending-thread added.
2:43
:7
4:702:
3
3:6 completed

2:24
3:52:
6
4::513
:4

3:342
:0:
4
4:33:2

Liftoff!
Thread joining...
4:32:Thread exiting...

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

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

LiftOff构造函数的第2个参数用于标识每个任务
当运行程序时将会看到
由于线程被不断地换入换出
以至于不同的任务被混合在一起执行 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值