POSIX线程
维基百科,自由的百科全书
POSIX线程是线程的POSIX标准,定义了创建和操纵线程的一套API。
实现POSIX 线程标准的库常被称作Pthreads,一般用于Unix-like POSIX 系统,如Linux、 Solaris。但是Microsoft Windows上的实现也存在,例如直接使用Windows API实现的第三方库pthreads-w32;而利用Windows的SFU/SUA子系统,则可以使用微软提供的一部分原生POSIX API。
目录 |
API具体内容 [编辑]
Pthreads定义了一套C语言的类型、函数与常量,它以pthread.h头文件和一个线程库实现。
Pthreads API中大致共有100个函数调用,全都以"pthread_"开头,并可以分为四类:
- 线程管理,例如创建线程,等待(join)线程等。
- Mutex
- Condition Variable
- 使用了读写锁的线程间的同步管理
POSIX的Semaphore API可以和Pthreads协同工作,但这并不是Pthreads的标准。因而这部分API是以"sem_"打头,而非"pthread_"。
数据类型
- pthread_t:线程句柄
- pthread_attr_t:线程属性
线程操纵函数(简介起见,省略参数):
- pthread_create():创建一个线程
- pthread_exit():终止当前线程
- pthread_cancel():中断另外一个线程的运行
- pthread_join():阻塞当前的线程,直到另外一个线程运行结束
- pthread_attr_init():初始化线程的属性
- pthread_attr_setdetachstate():设置脱离状态的属性(决定这个线程在终止时是否可以被结合)
- pthread_attr_getdetachstate():获取脱离状态的属性
- pthread_attr_destroy():删除线程的属性
- pthread_kill():向线程发送一个信号
- pthread_mutex_init() 初始化互斥锁
- pthread_mutex_destroy() 删除互斥锁
- pthread_mutex_lock():占有互斥锁(阻塞操作)
- pthread_mutex_trylock():试图占有互斥锁(不阻塞操作)。即,当互斥锁空闲时,将占有该锁;否则,立即返回。
- pthread_mutex_unlock(): 释放互斥锁
- pthread_cond_init():初始化条件变量
- pthread_cond_destroy():销毁条件变量
- pthread_cond_signal(): 唤醒第一个调用 pthread_cond_wait()而进入睡眠的线程
- pthread_cond_wait(): 等待条件变量的特殊条件发生
Thread-local storage(或者以Pthreads术语,称作 线程特有数据):
- pthread_key_create(): 分配用于标识进程中线程特定数据的键
- pthread_setspecific(): 为指定线程特定数据键设置线程特定绑定
- pthread_getspecific(): 获取调用线程的键绑定,并将该绑定存储在 value 指向的位置中
- pthread_key_delete(): 销毁现有线程特定数据键
与一起工作的工具函数:
- pthread_equal(): 对两个线程的线程标识号进行比较
- pthread_detach(): 分离线程
- pthread_self(): 查询线程自身线程标识号
Example [编辑]
C中使用 Pthreads的示例:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h> static void wait(void) { time_t start_time = time(NULL); while (time(NULL) == start_time) { /* do nothing except chew CPU slices for up to one second */ } } static void *thread_func(void *vptr_args) { int i; for (i = 0; i < 20; i++) { fputs(" b\n", stderr); wait(); } return NULL; } int main(void) { int i; pthread_t thread; if (pthread_create(&thread, NULL, thread_func, NULL) != 0) { return EXIT_FAILURE; } for (i = 0; i < 20; i++) { puts("a"); wait(); } if (pthread_join(thread, NULL) != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
这段程序创建了一个新线程,打印含有“b”的行,主线程打印含有“a”的行。当两个线程相互切换执行时输出结果为'a'和'b'交替出现。 More tutorials can be found below in the links section.
参考 [编辑]
- David R. Butenhof:Programming with POSIX Threads, Addison-Wesley, ISBN 0-201-63392-2
- Bradford Nichols,Dick Buttlar,Jacqueline Proulx Farell:Pthreads Programming, O'Reilly & Associates, ISBN 1-56592-115-1
- Charles J. Northrup:Programming with UNIX Threads, John Wiley & Sons, ISBN 0-471-13751-0
- Kay A. Robbins and Steven Robbins,UNIX Systems Programming, Prentice-Hall, ISBN 0-13-042411-0
参见 [编辑]
- Native POSIX Thread Library (NPTL)
- Spurious wakeup
- Thread-local storage
- GNU Portable Threads
- FSU Pthreads
外部链接 [编辑]
- Pthread Win-32,Basic Programming
- Pthreads Tutorial
- C/C++ Tutorial: using Pthreads
- Article "POSIX threads explained" by Daniel Robbins (Gentoo Linux founder)
- Interview "Ten Questions with David Butenhof about Parallel Programming and POSIX Threads" by Michael Suess
- Open Source POSIX Threads for Win32
- The Open Group Base Specifications Issue 6, IEEE Std 1003.1
- GNU Portable threads
- Pthreads Presentation at 2007 OSCON (O'Reilly Open Source Convention) by Adrien Lamothe. An overview of Pthreads with current trends.
|
|||||||||||||||||||||||||||||||