下面简要论述了特定任务及其相关手册页。mysql
若是未指定属性对象,则该对象为 NULL,系统会建立具备如下属性的缺省线程:程序员
进程范围sql
非分离数据库
缺省栈和缺省栈大小安全
零优先级多线程
还能够用 pthread_attr_init() 建立缺省属性对象,而后使用该属性对象来建立缺省线程。有关详细信息,请参见初始化属性一节。函数
使用 pthread_create(3C) 能够向当前进程中添加新的受控线程。fetch
int pthread_create(pthread_t *tid, const pthread_attr_t *tattr, void*(*start_routine)(void *), void *arg);
#include <pthread.h> pthread_attr_t() tattr; pthread_t tid; extern void *start_routine(void *arg); void *arg; int ret; /* default behavior*/ ret = pthread_create(&tid, NULL, start_routine, arg); /* initialized with default attributes */ ret = pthread_attr_init(&tattr); /* default behavior specified*/ ret = pthread_create(&tid, &tattr, start_routine, arg);
使用具备必要状态行为的 attr 调用 pthread_create() 函数。 start_routine 是新线程最早执行的函数。当 start_routine 返回时,该线程将退出,其退出状态设置为由 start_routine 返回的值。请参见pthread_create 语法。spa
当 pthread_create() 成功时,所建立线程的 ID 被存储在由 tid 指向的位置中。.net
使用 NULL 属性参数或缺省属性调用 pthread_create() 时,pthread_create() 会建立一个缺省线程。在对 tattr 进行初始化以后,该线程将得到缺省行为。
pthread_create() 在调用成功完成以后返回零。其余任何返回值都表示出现了错误。若是检测到如下任一状况,pthread_create() 将失败并返回相应的值。
EAGAIN
描述:
超出了系统限制,如建立的线程太多。
EINVAL
描述:
tattr 的值无效。
pthread_join() 函数会一直阻塞调用线程,直到指定的线程终止。
使用 pthread_join(3C) 等待线程终止。
int pthread_join(thread_t tid, void **status);
#include <pthread.h> pthread_t tid; int ret; void *status; /* waiting to join thread "tid" with status */ ret = pthread_join(tid, &status); /* waiting to join thread "tid" without status */ ret = pthread_join(tid, NULL);
指定的线程必须位于当前的进程中,并且不得是分离线程。有关线程分离的信息,请参见设置分离状态。
当 status 不是 NULL 时,status 指向某个位置,在 pthread_join() 成功返回时,将该位置设置为已终止线程的退出状态。
若是多个线程等待同一个线程终止,则全部等待线程将一直等到目标线程终止。而后,一个等待线程成功返回。其他的等待线程将失败并返回 ESRCH 错误。
在 pthread_join() 返回以后,应用程序可回收与已终止线程关联的任何数据存储空间。
调用成功完成后,pthread_join() 将返回零。其余任何返回值都表示出现了错误。若是检测到如下任一状况,pthread_join() 将失败并返回相应的值。
ESRCH
描述:
没有找到与给定的线程 ID 相对应的线程。
EDEADLK
描述:
将出现死锁,如一个线程等待其自己,或者线程 A 和线程 B 互相等待。
EINVAL
描述:
与给定的线程 ID 相对应的线程是分离线程。
pthread_join() 仅适用于非分离的目标线程。若是没有必要等待特定线程终止以后才进行其余处理,则应当将该线程分离。
在示例 2–1 中,一个线程执行位于顶部的过程,该过程首先建立一个辅助线程来执行 fetch() 过程。fetch() 执行复杂的数据库查找操做,查找过程须要花费一些时间。
主线程将等待查找结果,但同时还执行其余操做。所以,主线程将执行其余活动,而后经过执行 pthread_join() 等待辅助线程。
将新线程的 pbe 参数做为栈参数进行传递。这个线程参数之因此可以做为栈参数传递,是由于主线程会等待辅助线程终止。不过,首选方法是使用 malloc 从堆分配存储,而不是传递指向线程栈存储的地址。若是将该参数做为地址传递到线程栈存储,则该地址可能无效或者在线程终止时会被从新分配。
void mainline (...) { struct phonebookentry *pbe; pthread_attr_t tattr; pthread_t helper; void *status; pthread_create(&helper, NULL, fetch, &pbe); /* do something else for a while */ pthread_join(helper, &status); /* it's now safe to use result */ } void *fetch(struct phonebookentry *arg) { struct phonebookentry *npbe; /* fetch value from a database */ npbe = search (prog_name) if (npbe != NULL) *arg = *npbe; pthread_exit(0); } struct phonebookentry { char name[64]; char phonenumber[32]; char flags[16]; }
pthread_detach(3C) 是 pthread_join(3C) 的替代函数,可回收建立时 detachstate 属性设置为 PTHREAD_CREATE_JOINABLE 的线程的存储空间。
int pthread_detach(thread_t tid);
#include <pthread.h> pthread_t tid; int ret; /* detach thread tid */ ret = pthread_detach(tid);
pthread_detach() 函数用于指示应用程序在线程 tid 终止时回收其存储空间。若是 tid 还没有终止,pthread_detach() 不会终止该线程。
pthread_detach() 在调用成功完成以后返回零。其余任何返回值都表示出现了错误。若是检测到如下任一状况,pthread_detach() 将失败并返回相应的值。
EINVAL
描述:
tid 是分离线程。
ESRCH
描述:
tid 不是当前进程中有效的未分离的线程。
单线程 C 程序有两类基本数据:局部数据和全局数据。对于多线程 C 程序,添加了第三类数据:线程特定数据。线程特定数据与全局数据很是类似,区别在于前者为线程专有。
线程特定数据基于每线程进行维护。TSD(特定于线程的数据)是定义和引用线程专用数据的惟一方法。每一个线程特定数据项都与一个做用于进程内全部线程的键关联。经过使用 key,线程能够访问基于每线程进行维护的指针 (void *)。
int pthread_key_create(pthread_key_t *key, void (*destructor) (void *));
#include <pthread.h> pthread_key_t key; int ret; /* key create without destructor */ ret = pthread_key_create(&key, NULL); /* key create with destructor */ ret = pthread_key_create(&key, destructor);
可使用 pthread_key_create(3C) 分配用于标识进程中线程特定数据的键。键对进程中的全部线程来讲是全局的。建立线程特定数据时,全部线程最初都具备与该键关联的 NULL 值。
使用各个键以前,会针对其调用一次 pthread_key_create()。不存在对键(为进程中全部的线程所共享)的隐含同步。
建立键以后,每一个线程都会将一个值绑定到该键。这些值特定于线程而且针对每一个线程单独维护。若是建立该键时指定了 destructor 函数,则该线程终止时,系统会解除针对每线程的绑定。
当 pthread_key_create() 成功返回时,会将已分配的键存储在 key 指向的位置中。调用方必须确保对该键的存储和访问进行正确的同步。
使用可选的析构函数 destructor 能够释放过期的存储。若是某个键具备非 NULL destructor 函数,而线程具备一个与该键关联的非 NULL 值,则该线程退出时,系统将使用当前的相关值调用 destructor 函数。destructor 函数的调用顺序不肯定。
pthread_key_create() 在成功完成以后返回零。其余任何返回值都表示出现了错误。若是出现如下任一状况,pthread_key_create() 将失败并返回相应的值。
EAGAIN
描述:
key 名称空间已经用完。
ENOMEM
描述:
此进程中虚拟内存不足,没法建立新键。
使用 pthread_key_delete(3C) 能够销毁现有线程特定数据键。因为键已经无效,所以将释放与该键关联的全部内存。引用无效键将返回错误。Solaris 线程中没有相似的函数。
int pthread_key_delete(pthread_key_t key);
#include <pthread.h> pthread_key_t key; int ret; /* key previously created */ ret = pthread_key_delete(key);
若是已删除键,则使用调用 pthread_setspecific() 或 pthread_getspecific() 引用该键时,生成的结果将是不肯定的。
程序员在调用删除函数以前必须释放全部线程特定资源。删除函数不会调用任何析构函数。反复调用 pthread_key_create() 和 pthread_key_delete() 可能会产生问题。若是 pthread_key_delete() 将键标记为无效,而以后 key 的值再也不被重用,那么反复调用它们就会出现问题。对于每一个所需的键,应当只调用 pthread_key_create() 一次。
pthread_key_delete() 在成功完成以后返回零。其余任何返回值都表示出现了错误。若是出现如下状况,pthread_key_delete() 将失败并返回相应的值。
EINVAL
描述:
key 的值无效。
使用 pthread_setspecific(3C) 能够为指定线程特定数据键设置线程特定绑定。
int pthread_setspecific(pthread_key_t key, const void *value);
#include <pthread.h> pthread_key_t key; void *value; int ret; /* key previously created */ ret = pthread_setspecific(key, value);
pthread_setspecific() 在成功完成以后返回零。其余任何返回值都表示出现了错误。若是出现如下任一状况,pthread_setspecific() 将失败并返回相应的值。
ENOMEM
描述:
虚拟内存不足。
EINVAL
描述:
key 无效。
注 –
设置新绑定时,pthread_setspecific() 不会释放其存储空间。必须释放现有绑定,不然会出现内存泄漏。
请使用 pthread_getspecific(3C) 获取调用线程的键绑定,并将该绑定存储在 value 指向的位置中。
void *pthread_getspecific(pthread_key_t key);
#include <pthread.h> pthread_key_t key; void *value; /* key previously created */ value = pthread_getspecific(key);
pthread_getspecific 不返回任何错误。
示例 2–2 显示的代码是从多线程程序中摘录出来的。这段代码能够由任意数量的线程执行,但该代码引用了两个全局变量:errno 和 mywindow。这些全局值实际上应当是对每一个线程专用项的引用。
body() { ... while (write(fd, buffer, size) == -1) { if (errno != EINTR) { fprintf(mywindow, "%s\n", strerror(errno)); exit(1); } } ... }
errno 引用应该从线程所调用的例程获取系统错误,而从其余线程所调用的例程获取系统错误。所以,线程不一样,引用 errno 所指向的存储位置也不一样。
mywindow 变量指向一个 stdio (标准 IO)流,做为线程专属的流窗口。所以,与 errno 同样,线程不一样,引用 mywindow 所指向的存储位置也不一样。最终,这个引用指向不一样的流窗口。惟一的区别在于系统负责处理 errno,而程序员必须处理对 mywindow 的引用。
下一个示例说明对 mywindow 的引用如何工做。预处理程序会将对 mywindow 的引用转换为对 _mywindow() 过程的调用。
此例程随后调用 pthread_getspecific()。pthread_getspecific() 接收 mywindow_key 全局变量做为输入参数,以输出参数 win 返回该线程的窗口。
thread_key_t mywin_key; FILE *_mywindow(void) { FILE *win; win = pthread_getspecific(mywin_key); return(win); } #define mywindow _mywindow() void routine_uses_win( FILE *win) { ... } void thread_start(...) { ... make_mywin(); ... routine_uses_win( mywindow ) ... }
mywin_key 变量标识一类变量,对于该类变量,每一个线程都有其各自的专用副本。这些变量是线程特定数据。每一个线程都调用 make_mywin() 以初始化其时限并安排其 mywindow 实例以引用线程特定数据。
调用此例程以后,此线程能够安全地引用 mywindow,调用 _mywindow() 以后,此线程将得到对其专用时限的引用。引用 mywindow 相似于直接引用线程专用数据。
示例 2–4 说明如何设置引用。
void make_mywindow(void) { FILE **win; static pthread_once_t mykeycreated = PTHREAD_ONCE_INIT; pthread_once(&mykeycreated, mykeycreate); win = malloc(sizeof(*win)); create_window(win, ...); pthread_setspecific(mywindow_key, win); } void mykeycreate(void) { pthread_key_create(&mywindow_key, free_key); } void free_key(void *win) { free(win); }
首先,获得一个惟一的键值,mywin_key。此键用于标识线程特定数据类。第一个调用 make_mywin() 的线程最终会调用 pthread_key_create(),该函数将惟一的 key 赋给其第一个参数。第二个参数是 destructor 函数,用于在线程终止后将该线程的特定于该线程的数据项实例解除分配。
接下来为调用方的线程特定数据项的实例分配存储空间。获取已分配的存储空间,调用 create_window(),以便为该线程设置时限。win 指向为该时限分配的存储空间。最后,调用 pthread_setspecific(),将 win 与该键关联。
之后,每当线程调用 pthread_getspecific() 以传递全局 key,线程都会得到它在前一次调用 pthread_setspecific() 时设置的与该键关联的值)。
线程终止时,会调用在 pthread_key_create() 中设置的 destructor 函数。每一个 destructor 函数仅在终止线程经过调用 pthread_setspecific() 为 key 赋值以后才会被调用。