多线程编程指南
Sun Microsystems, Inc.
4150 Network Circle
Santa Clara, CA 95054
U.S.A.
文件号码 819–7051–10
2006 年 10 月
版权所有 2005 Sun Microsystems, Inc. 4150 Network Circle, Santa Clara, CA95054 U.S.A. 保留所有权利。
本文档及其相关产品的使用、复制、分发和反编译均受许可证限制。未经 Sun 及其许可方(如果有)的事先书面许可,不得以任何形式、任何手段复
制本产品或文档的任何部分。第三方软件,包括字体技术,均已从 Sun 供应商处获得版权和使用许可。
本产品的某些部分可能是从 Berkeley BSD 系统衍生出来的,并获得了加利福尼亚大学的许可。UNIX 是 X/Open Company, Ltd. 在美国和其他国家/地区
独家许可的注册商标。
Sun、Sun Microsystems、Sun 徽标、docs.sun.com、AnswerBook、AnswerBook2、和Solaris 是 Sun Microsystems, Inc. 在美国和其他国家/地区的商标或注
册商标。所有 SPARC 商标的使用均已获得许可,它们是 SPARC International, Inc. 在美国和其他国家/地区的商标或注册商标。标有 SPARC 商标的产品
均基于由 Sun Microsystems, Inc. 开发的体系结构。
OPEN LOOK 和 Sun
TM
图形用户界面是 Sun Microsystems, Inc. 为其用户和许可证持有者开发的。Sun 感谢 Xerox 在研究和开发可视或图形用户界面的概
念方面为计算机行业所做的开拓性贡献。Sun 已从 Xerox 获得了对 Xerox 图形用户界面的非独占性许可证,该许可证还适用于实现 OPEN LOOK GUI 和
在其他方面遵守 Sun 书面许可协议的 Sun 许可证持有者。
美国政府权利-商业软件。政府用户应遵循 Sun Microsystems, Inc. 的标准许可协议,以及 FAR(Federal Acquisition Regulations,即“联邦政府采购法
规”)的适用条款及其补充条款。
本文档按“原样”提供,对于所有明示或默示的条件、陈述和担保,包括对适销性、适用性或非侵权性的默示保证,均不承担任何责任,除非此免责声
明的适用范围在法律上无效。
061017@15490
目录
前言 ................................................................................................................................................................ 11
1 多线程基础介绍 ..........................................................................................................................................15
定义多线程术语 ..........................................................................................................................................15
符合多线程标准 ..........................................................................................................................................16
多线程的益处 ..............................................................................................................................................17
提高应用程序的响应 .........................................................................................................................17
有效使用多处理器 ..............................................................................................................................17
改进程序结构 ......................................................................................................................................17
占用较少的系统资源 .........................................................................................................................17
结合线程和 RPC(远程过程调用) ...............................................................................................18
多线程概念 ...................................................................................................................................................18
并发性和并行性 ..................................................................................................................................18
多线程结构一览 ..................................................................................................................................18
线程调度 ...............................................................................................................................................19
线程取消 ...............................................................................................................................................19
线程同步 ...............................................................................................................................................20
使用 64 位体系结构 ....................................................................................................................................20
2 基本线程编程 ..............................................................................................................................................23
线程库 ............................................................................................................................................................23
创建缺省线程 ......................................................................................................................................23
等待线程终止 ......................................................................................................................................25
简单线程的示例 ..................................................................................................................................26
分离线程 ...............................................................................................................................................28
为线程特定数据创建键 .....................................................................................................................28
删除线程特定数据键 .........................................................................................................................30
设置线程特定数据 ..............................................................................................................................31
3
获取线程特定数据 ..............................................................................................................................31
获取线程标识符 ..................................................................................................................................35
比较线程 ID ..........................................................................................................................................36
初始化线程 ...........................................................................................................................................36
停止执行线程 ......................................................................................................................................37
设置线程的优先级 ..............................................................................................................................38
获取线程的优先级 ..............................................................................................................................39
向线程发送信号 ..................................................................................................................................40
访问调用线程的信号掩码 .................................................................................................................41
安全地 Fork ..........................................................................................................................................41
终止线程 ...............................................................................................................................................42
结束 ........................................................................................................................................................42
取消线程 ...............................................................................................................................................43
取消线程 ...............................................................................................................................................44
启用或禁用取消功能 .........................................................................................................................45
设置取消类型 ......................................................................................................................................46
创建取消点 ...........................................................................................................................................46
将处理程序推送到栈上 .....................................................................................................................47
从栈中弹出处理程序 .........................................................................................................................47
3 线程属性 .......................................................................................................................................................49
属性对象 .......................................................................................................................................................49
初始化属性 ...........................................................................................................................................50
销毁属性 ...............................................................................................................................................51
设置分离状态 ......................................................................................................................................52
获取分离状态 ......................................................................................................................................53
设置栈溢出保护区大小 .....................................................................................................................54
获取栈溢出保护区大小 .....................................................................................................................55
设置范围 ...............................................................................................................................................55
获取范围 ...............................................................................................................................................57
设置线程并行级别 ..............................................................................................................................57
获取线程并行级别 ..............................................................................................................................58
设置调度策略 ......................................................................................................................................58
获取调度策略 ......................................................................................................................................59
设置继承的调度策略 .........................................................................................................................60
获取继承的调度策略 .........................................................................................................................61
目录
多线程编程指南 • 2006 年 10 月4
设置调度参数 ......................................................................................................................................62
获取调度参数 ......................................................................................................................................63
设置栈大小 ...........................................................................................................................................65
获取栈大小 ...........................................................................................................................................66
关于栈 ....................................................................................................................................................67
设置栈地址和大小 ..............................................................................................................................68
获取栈地址和大小 ..............................................................................................................................70
4 用同步对象编程 ..........................................................................................................................................73
互斥锁属性 ...................................................................................................................................................74
初始化互斥锁属性对象 .....................................................................................................................75
销毁互斥锁属性对象 .........................................................................................................................76
设置互斥锁的范围 ..............................................................................................................................76
获取互斥锁的范围 ..............................................................................................................................77
设置互斥锁类型的属性 .....................................................................................................................78
获取互斥锁的类型属性 .....................................................................................................................79
设置互斥锁属性的协议 .....................................................................................................................80
获取互斥锁属性的协议 .....................................................................................................................82
设置互斥锁属性的优先级上限 ........................................................................................................83
获取互斥锁属性的优先级上限 ........................................................................................................84
设置互斥锁的优先级上限 .................................................................................................................84
获取互斥锁的优先级上限 .................................................................................................................85
设置互斥锁的强健属性 .....................................................................................................................86
获取互斥锁的强健属性 .....................................................................................................................88
使用互斥锁 ...................................................................................................................................................89
初始化互斥锁 ......................................................................................................................................89
使互斥保持一致 ..................................................................................................................................90
锁定互斥锁 ...........................................................................................................................................91
解除锁定互斥锁 ..................................................................................................................................93
使用非阻塞互斥锁锁定 .....................................................................................................................94
销毁互斥锁 ...........................................................................................................................................95
互斥锁定的代码示例 .........................................................................................................................96
条件变量属性 ............................................................................................................................................102
初始化条件变量属性 .......................................................................................................................103
删除条件变量属性 ............................................................................................................................103
设置条件变量的范围 .......................................................................................................................104
目录
5