资源介绍
多线程基础介绍.........................................................................................................................................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
目录
4 多线程编程指南• 2006年10月
设置调度参数.....................................................................................................................................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
获取条件变量的范围.......................................................................................................................105
使用条件变量...........................................................................................................................................106
初始化条件变量...............................................................................................................................106
基于条件变量阻塞............................................................................................................................108
解除阻塞一个线程............................................................................................................................109
在指定的时间之前阻塞................................................................................................................... 111
在指定的时间间隔内阻塞............................................................................................................... 113
解除阻塞所有线程............................................................................................................................ 114
销毁条件变量状态............................................................................................................................ 116
唤醒丢失问题................................................................................................................................... 117
生成方和使用者问题....................................................................................................................... 117
使用信号进行同步...................................................................................................................................121
命名信号和未命名信号...................................................................................................................122
计数信号量概述...............................................................................................................................122
初始化信号........................................................................................................................................123
增加信号............................................................................................................................................125
基于信号计数进行阻塞...................................................................................................................126
减小信号计数...................................................................................................................................126
销毁信号状态...................................................................................................................................127
使用信号时的生成方和使用者问题.............................................................................................128
读写锁属性................................................................................................................................................130
初始化读写锁属性............................................................................................................................131
销毁读写锁属性...............................................................................................................................131
设置读写锁属性...............................................................................................................................132
获取读写锁属性...............................................................................................................................132
使用读写锁................................................................................................................................................133
初始化读写锁...................................................................................................................................133
获取读写锁中的读锁.......................................................................................................................134
读取非阻塞读写锁中的锁...............................................................................................................135
写入读写锁中的锁............................................................................................................................136
写入非阻塞读写锁中的锁...............................................................................................................136
解除锁定读写锁...............................................................................................................................137
销毁读写锁........................................................................................................................................137
跨进程边界同步.......................................................................................................................................138
生成方和使用者问题示例...............................................................................................................138
比较元语....................................................................................................................................................141
目录
6 多线程编程指南• 2006年10月
5 使用Solaris 软件编程..............................................................................................................................143
进程创建中的fork 问题...........................................................................................................................143
Fork-One 模型...................................................................................................................................144
Fork-all 模型......................................................................................................................................147
选择正确的Fork ...............................................................................................................................147
进程创建:exec 和exit 问题................................................................................................................147
计时器、报警与剖析...............................................................................................................................148
每LWP POSIX 计时器......................................................................................................................148
每线程报警........................................................................................................................................148
剖析多线程程序...............................................................................................................................149
非本地转向:setjmp 和longjmp ...........................................................................................................149
资源限制....................................................................................................................................................149
LWP 和调度类...........................................................................................................................................149
分时调度............................................................................................................................................150
实时调度............................................................................................................................................150
公平共享调度程序............................................................................................................................151
固定优先级调度...............................................................................................................................151
扩展传统信号...........................................................................................................................................151
同步信号............................................................................................................................................152
异步信号............................................................................................................................................152
延续语义............................................................................................................................................152
对信号执行的操作............................................................................................................................154
定向于线程的信号............................................................................................................................155
完成语义............................................................................................................................................157
信号处理程序和异步信号安全......................................................................................................158
中断对条件变量的等待...................................................................................................................160
I/O 问题......................................................................................................................................................161
I/O 作为远程过程调用.....................................................................................................................161
人为的异步性...................................................................................................................................162
异步I/O ..............................................................................................................................................162
共享的I/O 和新的I/O 系统调用...................................................................................................163
getc 和putc 的替代项......................................................................................................................164
6 安全和不安全的接口...............................................................................................................................165
线程安全....................................................................................................................................................165
MT接口安全级别....................................................................................................................................167
目录
7
不安全接口的可重复执行函数......................................................................................................168
异步信号安全函数...................................................................................................................................168
库的MT安全级别...................................................................................................................................169
不安全库............................................................................................................................................169
7 编译和调试................................................................................................................................................171
编译多线程应用程序...............................................................................................................................171
为编译做准备...................................................................................................................................171
选择Solaris 语义或POSIX 语义.....................................................................................................171
包括 或 ...................................................................................................172
定义_REENTRANT 或_POSIX_C_SOURCE ...........................................................................................173
使用libthread 或libpthread 链接..............................................................................................173
与POSIX 信号的-lrt 链接.............................................................................................................174
将原有模块与新模块链接...............................................................................................................174
备用线程库................................................................................................................................................175
调试多线程程序.......................................................................................................................................175
多线程程序中常见的疏忽性问题.................................................................................................175
使用TNF 实用程序跟踪和调试.....................................................................................................176
使用truss ..........................................................................................................................................176
使用mdb ..............................................................................................................................................176
使用dbx ..............................................................................................................................................177
8 Solaris 线程编程.......................................................................................................................................179
比较Solaris 线程和POSIX 线程的API .................................................................................................179
API 的主要差异................................................................................................................................179
函数比较表........................................................................................................................................180
Solaris 线程的独有函数...........................................................................................................................183
暂停执行线程...................................................................................................................................183
继续执行暂停的线程.......................................................................................................................185
相似的同步函数-读写锁......................................................................................................................186
初始化读写锁...................................................................................................................................186
获取读锁............................................................................................................................................188
尝试获取读锁...................................................................................................................................188
获取写锁............................................................................................................................................189
尝试获取写锁...................................................................................................................................189
解除锁定读写锁...............................................................................................................................190
目录
8 多线程编程指南• 2006年10月
销毁读写锁的状态............................................................................................................................191
相似的Solaris 线程函数...........................................................................................................................193
创建线程............................................................................................................................................193
获取最小栈大小...............................................................................................................................195
获取线程标识符...............................................................................................................................196
停止执行线程...................................................................................................................................196
向线程发送信号...............................................................................................................................197
访问调用线程的信号掩码...............................................................................................................197
终止线程............................................................................................................................................198
等待线程终止...................................................................................................................................198
创建线程特定的数据键...................................................................................................................200
设置线程特定的数据值...................................................................................................................201
获取线程特定的数据值...................................................................................................................201
设置线程的优先级............................................................................................................................202
获取线程的优先级............................................................................................................................203
相似的同步函数-互斥锁......................................................................................................................204
初始化互斥锁...................................................................................................................................204
销毁互斥锁........................................................................................................................................206
获取互斥锁........................................................................................................................................207
释放互斥锁........................................................................................................................................207
尝试获取互斥锁...............................................................................................................................208
相似的同步函数:条件变量..................................................................................................................208
初始化条件变量...............................................................................................................................208
销毁条件变量...................................................................................................................................210
等待条件............................................................................................................................................210
等待绝对时间................................................................................................................................... 211
等待时间间隔...................................................................................................................................212
解除阻塞一个线程............................................................................................................................213
解除阻塞所有线程............................................................................................................................213
相似的同步函数:信号...........................................................................................................................214
初始化信号........................................................................................................................................214
增加信号............................................................................................................................................215
基于信号计数阻塞............................................................................................................................216
减小信号计数...................................................................................................................................216
销毁信号状态...................................................................................................................................217
跨进程边界同步.......................................................................................................................................218
生成方和使用者问题示例...............................................................................................................218
目录
9
fork() 和Solaris 线程的特殊问题.........................................................................................................220
9 编程原则....................................................................................................................................................221
重新考虑全局变量...................................................................................................................................221
提供静态局部变量...................................................................................................................................222
同步线程....................................................................................................................................................223
单线程策略........................................................................................................................................224
可重复执行函数...............................................................................................................................224
避免死锁....................................................................................................................................................226
与调用相关的死锁............................................................................................................................227
锁定原则............................................................................................................................................227
线程代码的一些基本原则......................................................................................................................227
创建和使用线程.......................................................................................................................................228
使用多处理器...........................................................................................................................................228
基础体系结构...................................................................................................................................229
线程程序示例...........................................................................................................................................233
需要进一步阅读的内容...................................................................................................................233
A 样例应用程序:多线程grep ..................................................................................................................235
tgrep 的说明.............................................................................................................................................235
B Solaris 线程示例: barrier.c ................................................................................................................293
索引.............................................................................................................................................................303