-
指南:Solaris的链接程序与库使用
资源介绍
《多线程编程指南》介绍了SolarisTM操(SolarisOperatingSystem,SolarisOS中
POSIX®线程和Solaris线程的多线程编程接口。本指南将指导应用程序程序员如何创建新的多线程程序以及如何向现有的程序中添加多线程。尽管本指南同时介绍POSIX线程接口和Solaris线程接口,但大多数主题都以POSIX线程为重点。仅适用Solaris线程的信息将专门在一章中介绍。
目录
前言........................................................................................................................................................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
为线程特定数据创建键 ............................................................................................................ 29
删除线程特定数据键................................................................................................................. 30
3
设置线程特定数据 ..................................................................................................................... 31
获取线程特定数据 ..................................................................................................................... 32
获取线程标识符 ......................................................................................................................... 36
比较线程ID ................................................................................................................................. 36
初始化线程 .................................................................................................................................. 37
停止执行线程 .............................................................................................................................. 38
设置线程的优先级 ..................................................................................................................... 38
获取线程的优先级 ..................................................................................................................... 39
向线程发送信号 ......................................................................................................................... 40
访问调用线程的信号掩码 ........................................................................................................ 41
安全地Fork .................................................................................................................................. 42
终止线程 ...................................................................................................................................... 42
结束 ............................................................................................................................................... 43
取消线程 ...................................................................................................................................... 43
取消线程 ...................................................................................................................................... 45
启用或禁用取消功能................................................................................................................. 45
设置取消类型 .............................................................................................................................. 46
创建取消点 .................................................................................................................................. 47
将处理程序推送到栈上 ............................................................................................................ 48
从栈中弹出处理程序................................................................................................................. 48
3
线程属性 ...............................................................................................................................................51
属性对象 .............................................................................................................................................. 51
初始化属性 .................................................................................................................................. 52
销毁属性 ...................................................................................................................................... 53
设置分离状态 .............................................................................................................................. 54
获取分离状态 .............................................................................................................................. 55
设置栈溢出保护区大小 ............................................................................................................ 56
获取栈溢出保护区大小 ............................................................................................................ 57
设置范围 ...................................................................................................................................... 58
获取范围 ...................................................................................................................................... 59
设置线程并行级别 ..................................................................................................................... 60
获取线程并行级别 ..................................................................................................................... 60
设置调度策略 .............................................................................................................................. 61
获取调度策略 .............................................................................................................................. 62
目录
多线程编程指南 • 2006年10月
4
设置继承的调度策略................................................................................................................. 63
获取继承的调度策略................................................................................................................. 64
设置调度参数 .............................................................................................................................. 64
获取调度参数 .............................................................................................................................. 65
设置栈大小 .................................................................................................................................. 67
获取栈大小 .................................................................................................................................. 68
关于栈 ........................................................................................................................................... 69
设置栈地址和大小 ..................................................................................................................... 71
获取栈地址和大小 ..................................................................................................................... 73
4
用同步对象编程 ..................................................................................................................................75
互斥锁属性.......................................................................................................................................... 76
初始化互斥锁属性对象 ............................................................................................................ 77
销毁互斥锁属性对象................................................................................................................. 78
设置互斥锁的范围 ..................................................................................................................... 79
获取互斥锁的范围 ..................................................................................................................... 80
设置互斥锁类型的属性 ............................................................................................................ 80
获取互斥锁的类型属性 ............................................................................................................ 82
设置互斥锁属性的协议 ............................................................................................................ 82
获取互斥锁属性的协议 ............................................................................................................ 84
设置互斥锁属性的优先级上限 ............................................................................................... 85
获取互斥锁属性的优先级上限 ............................................................................................... 86
设置互斥锁的优先级上限 ........................................................................................................ 87
获取互斥锁的优先级上限 ........................................................................................................ 88
设置互斥锁的强健属性 ............................................................................................................ 89
获取互斥锁的强健属性 ............................................................................................................ 90
使用互斥锁.......................................................................................................................................... 91
初始化互斥锁 .............................................................................................................................. 92
使互斥保持一致 ......................................................................................................................... 93
锁定互斥锁 .................................................................................................................................. 94
解除锁定互斥锁 ......................................................................................................................... 96
使用非阻塞互斥锁锁定 ............................................................................................................ 97
销毁互斥锁 .................................................................................................................................. 98
互斥锁定的代码示例................................................................................................................. 99
条件变量属性 ...................................................................................................................................105
目录
5
初始化条件变量属性...............................................................................................................106
删除条件变量属性 ...................................................................................................................106
设置条件变量的范围...............................................................................................................107
获取条件变量的范围...............................................................................................................108
使用条件变量 ...................................................................................................................................109
初始化条件变量 .......................................................................................................................109
基于条件变量阻塞 ...................................................................................................................111
解除阻塞一个线程 ...................................................................................................................112
在指定的时间之前阻塞 ..........................................................................................................114
在指定的时间间隔内阻塞 ......................................................................................................116
解除阻塞所有线程 ...................................................................................................................117
销毁条件变量状态 ...................................................................................................................119
唤醒丢失问题 ............................................................................................................................120
生成方和使用者问题...............................................................................................................120
使用信号进行同步 ..........................................................................................................................124
命名信号和未命名信号 ..........................................................................................................125
计数信号量概述 .......................................................................................................................125
初始化信号 ................................................................................................................................126
增加信号 ....................................................................................................................................128
基于信号计数进行阻塞 ..........................................................................................................129
减小信号计数 ............................................................................................................................129
销毁信号状态 ............................................................................................................................130
使用信号时的生成方和使用者问题 ....................................................................................131
读写锁属性........................................................................................................................................133
初始化读写锁属性 ...................................................................................................................134
销毁读写锁属性 .......................................................................................................................134
设置读写锁属性 .......................................................................................................................135
获取读写锁属性 .......................................................................................................................136
使用读写锁........................................................................................................................................136
初始化读写锁 ............................................................................................................................137
获取读写锁中的读锁...............................................................................................................138
读取非阻塞读写锁中的锁 ......................................................................................................139
写入读写锁中的锁 ...................................................................................................................139
写入非阻塞读写锁中的锁 ......................................................................................................140
解除锁定读写锁 .......................................................................................................................140
销毁读写锁 ................................................................................................................................141
目录
多线程编程指南 • 2006年10月
6
跨进程边界同步 ...............................................................................................................................142
生成方和使用者问题示例 ......................................................................................................142
比较元语 ............................................................................................................................................145
5
使用Solaris软件编程 ......................................................................................................................147
进程创建中的fork问题 .................................................................................................................147
Fork-One模型 ...........................................................................................................................148
Fork-all模型 ..............................................................................................................................151
选择正确的Fork .......................................................................................................................151
进程创建:exec和exit问题 .......................................................................................................152
计时器、报警与剖析 ......................................................................................................................152
每LWPPOSIX计时器 .............................................................................................................152
每线程报警 ................................................................................................................................153
剖析多线程程序 .......................................................................................................................153
非本地转向:setjmp和longjmp ..................................................................................................154
资源限制 ............................................................................................................................................154
LWP和调度类 ..................................................................................................................................154
分时调度 ....................................................................................................................................155
实时调度 ....................................................................................................................................155
公平共享调度程序 ...................................................................................................................155
固定优先级调度 .......................................................................................................................156
扩展传统信号 ...................................................................................................................................156
同步信号 ....................................................................................................................................157
异步信号 ....................................................................................................................................157
延续语义 ....................................................................................................................................157
对信号执行的操作 ...................................................................................................................158
定向于线程的信号 ...................................................................................................................160
完成语义 ....................................................................................................................................162
信号处理程序和异步信号安全 .............................................................................................163
中断对条件变量的等待 ..........................................................................................................165
I/O问题 ..............................................................................................................................................166
I/O作为远程过程调用 ............................................................................................................167
人为的异步性 ............................................................................................................................167
异步I/O ......................................................................................................................................167
共享的I/O和新的I/O系统调用 ..........................................................................................169
目录
7
getc和putc的替代项 .............................................................................................................169
6
安全和不安全的接口 ...................................................................................................................... 171
线程安全 ............................................................................................................................................171
MT接口安全级别 ............................................................................................................................173
不安全接口的可重复执行函数 .............................................................................................174
异步信号安全函数 ..........................................................................................................................175
库的MT安全级别 ...........................................................................................................................175
不安全库 ....................................................................................................................................176
7
编译和调试........................................................................................................................................ 177
编译多线程应用程序 ......................................................................................................................177
为编译做准备 ............................................................................................................................177
选择Solaris语义或POSIX语义 ............................................................................................178
包括或 ..........................................................................................178
定义_REENTRANT或_POSIX_C_SOURCE ..................................................................................179
使用libthread或libpthread链接 .....................................................................................179
与POSIX信号的-lrt链接 ....................................................................................................181
将原有模块与新模块链接 ......................................................................................................181
备用线程库........................................................................................................................................181
调试多线程程序 ...............................................................................................................................181
多线程程序中常见的疏忽性问题.........................................................................................181
使用TNF实用程序跟踪和调试 ............................................................................................182
使用truss ..................................................................................................................................182
使用mdb ......................................................................................................................................182
使用dbx ......................................................................................................................................183
8
Solaris线程编程 ................................................................................................................................185
比较Solaris线程和POSIX线程的API .......................................................................................185
API的主要差异 ........................................................................................................................185
函数比较表 ................................................................................................................................186
Solaris线程的独有函数 ..................................................................................................................189
暂停执行线程 ............................................................................................................................190
继续执行暂停的线程...............................................................................................................191
目录
多线程编程指南 • 2006年10月
8
相似的同步函数-读写锁 .............................................................................................................192
初始化读写锁 ............................................................................................................................192
获取读锁 ....................................................................................................................................194
尝试获取读锁 ............................................................................................................................194
获取写锁 ....................................................................................................................................195
尝试获取写锁 ............................................................................................................................196
解除锁定读写锁 .......................................................................................................................196
销毁读写锁的状态 ...................................................................................................................197
相似的Solaris线程函数 .................................................................................................................199
创建线程 ....................................................................................................................................199
获取最小栈大小 .......................................................................................................................201
获取线程标识符 .......................................................................................................................202
停止执行线程 ............................................................................................................................202
向线程发送信号 .......................................................................................................................203
访问调用线程的信号掩码 ......................................................................................................203
终止线程 ....................................................................................................................................204
等待线程终止 ............................................................................................................................204
创建线程特定的数据键 ..........................................................................................................206
设置线程特定的数据值 ..........................................................................................................207
获取线程特定的数据值 ..........................................................................................................208
设置线程的优先级 ...................................................................................................................208
获取线程的优先级 ...................................................................................................................210
相似的同步函数-互斥锁 .............................................................................................................210
初始化互斥锁 ............................................................................................................................210
销毁互斥锁 ................................................................................................................................213
获取互斥锁 ................................................................................................................................213
释放互斥锁 ................................................................................................................................214
尝试获取互斥锁 .......................................................................................................................214
相似的同步函数:条件变量 .........................................................................................................215
初始化条件变量 .......................................................................................................................215
销毁条件变量 ............................................................................................................................216
等待条件 ....................................................................................................................................217
等待绝对时间 ............................................................................................................................218
等待时间间隔 ............................................................................................................................218
解除阻塞一个线程 ...................................................................................................................219
解除阻塞所有线程 ...................................................................................................................220
目录
9
相似的同步函数:信号..................................................................................................................220
初始化信号 ................................................................................................................................220
增加信号 ....................................................................................................................................222
基于信号计数阻塞 ...................................................................................................................222
减小信号计数 ............................................................................................................................223
销毁信号状态 ............................................................................................................................224
跨进程边界同步 ...............................................................................................................................224
生成方和使用者问题示例 ......................................................................................................224
fork()和Solaris线程的特殊问题................................................................................................227
9
编程原则 ............................................................................................................................................ 229
重新考虑全局变量 ..........................................................................................................................229
提供静态局部变量 ..........................................................................................................................230
同步线程 ............................................................................................................................................231
单线程策略 ................................................................................................................................232
可重复执行函数 .......................................................................................................................232
避免死锁 ............................................................................................................................................234
与调用相关的死锁 ...................................................................................................................235
锁定原则 ....................................................................................................................................235
线程代码的一些基本原则 .............................................................................................................236
创建和使用线程 ...............................................................................................................................236
使用多处理器 ...................................................................................................................................237
基础体系结构 ............................................................................................................................237
线程程序示例 ...................................................................................................................................241
需要进一步阅读的内容 ..........................................................................................................241
A
样例应用程序:多线程grep .........................................................................................................243
tgrep的说明 .....................................................................................................................................243
B
Solaris线程示例:barrier.c........................................................................................................301
- 上一篇: SPARC_T3-1_服务器_安装指南
- 下一篇: Sun SPARC 处理器