基本线程编程(多线程编程笔记)

这篇博客详细介绍了多线程编程,包括创建线程、等待线程终止、设置线程特定数据、线程的优先级管理以及取消线程等关键操作。通过示例代码解释了线程的使用和管理,是理解多线程编程的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程库

下面简要论述了特定任务及其相关手册页。

创建缺省线程

如果未指定属性对象,则该对象为NULL,系统会创建具有以下属性的缺省线程:
■ 进程范围
■ 非分离
■ 缺省栈和缺省栈大小
■ 零优先级
还可以用pthread_attr_init() 创建缺省属性对象,然后使用该属性对象来创建缺省线
程。
pthread_create语法
使用pthread_create(3C) 可以向当前进程中添加新的受控线程。

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()成功时,所创建线程的ID 被存储在由tid 指向的位置中。
使用NULL 属性参数或缺省属性调用pthread_create()时,pthread_create() 会创建一
个缺省线程。在对tattr 进行初始化之后,该线程将获得缺省行为。

pthread_create 返回值
pthread_create() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。
如果检测到以下任一情况,pthread_create() 将失败并返回相应的值。
EAGAIN
描述: 超出了系统限制,如创建的线程太多。
EINVAL
描述: tattr 的值无效。

等待线程终止

pthread_join() 函数会一直阻塞调用线程,直到指定的线程终止。
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()将返回零。其他任何返回值都表示出现了错误。如
果检测到以下任一情况,pthread_join()将失败并返回相应的值。
ESRCH
描述: 没有找到与给定的线程ID 相对应的线程。
EDEADLK
描述: 将出现死锁,如一个线程等待其本身,或者线程A 和线程B 互相等待。
EINVAL
描述: 与给定的线程ID 相对应的线程是分离线程。
pthread_join() 仅适用于非分离的目标线程。如果没有必要等待特定线程终止之后才
进行其他处理,则应当将该线程分离。

简单线程的示例

在示例1 中,一个线程执行位于顶部的过程,该过程首先创建一个辅助线程来执行
fetch()过程。fetch()执行复杂的数据库查找操作,查找过程需要花费一些时间。
主线程将等待查找结果,但同时还执行其他操作。因此,主线程将执行其他活动,然
后通过执行pthread_join() 等待辅助线程。
将新线程的pbe 参数作为栈参数进行传递。这个线程参数之所以能够作为栈参数传递,
是因为主线程会等待辅助线程终止。不过,首选方法是使用malloc 从堆分配存储,而
不是传递指向线程栈存储的地址。如果将该参数作为地址传递到线程栈存储,则该地
址可能无效或者在线程终止时会被重新分配。
示例1简单线程程序

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的线程的存储空间。
pthread_detach语法

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()在调用成功完成之后返回零。其他任何返回值都表示出现了错误。
如果检测到以下任一情况,pthread_detach()将失败并返回相应的值。
EINVAL
描述: tid 是分离线程。
ESRCH
描述: tid 不是当前进程中有效的未分离的线程。

为线程特定数据创建键

单线程C 程序有两类基本数据:局部数据和全局数据。对于多线程C 程序,添加了第
三类数据:线程特定数据。线程特定数据与全局数据非常相似,区别在于前者为线程
专有。
线程特定数据基于每线程进行维护。TSD(特定于线程的数据)是定义和引用线程专
用数据的唯一方法。每个线程特定数据项都与一个作用于进程内所有线程的键关联。
通过使用key,线程可以访问基于每线程进行维护的指针(void *)

pthread_key_create语法

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()在成功完成之后返回零。其他任何返回值都表示出现了错误。如
果出现以下任一情况,pthread_key_create()将失败并返回相应的值。
EAGAIN
描述: key 名称空间已经用完。
ENOMEM
描述: 此进程中虚拟内存不足,无法创建新键。

删除线程特定数据键

使用pthread_key_delete(3C)可以销毁现有线程特定数据键。由于键已经无效,因此
将释放与该键关联的所有内存。引用无效键将返回错误。Solaris 线程中没有类似的函
数。
pthread_key_delete 语法

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()在成功完成之后返回零。其他任何返回值都表示出现了错误。如
果出现以下情况,pthread_key_delete() 将失败并返回相应的值。
EINVAL
描述: key 的值无效。

设置线程特定数据

使用pthread_setspecific(3C)可以为指定线程特定数据键设置线程特定绑定。
pthread_setspecific语法

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()在成功完成之后返回零。其他任何返回值都表示出现了错误。
如果出现以下任一情况,pthread_setspecific()将失败并返回相应的值。
ENOMEM
描述: 虚拟内存不足。
EINVAL
描述: key 无效。

获取线程特定数据

请使用pthread_getspecific(3C) 获取调用线程的键绑定,并将该绑定存储在value 指
向的位置中。
pthread_getspecific语法

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返回值
pthread_getspecific 不返回任何错误。

全局和专用线程特定数据的示例

示例2 显示的代码是从多线程程序中摘录出来的。这段代码可以由任意数量的线程
执行,但该代码引用了两个全局变量:errno 和mywindow。这些全局值实际上应当是
对每个线程专用项的引用。
示例2线程特定数据-全局但专用

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 返回该线程的窗口。
示例3将全局引用转化为专用引用

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 类似于直接引用线程专用数据。
示例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 赋值之后才会被
调用。

获取线程标识符

请使用pthread_self(3C)获取调用线程的thread identifier。
pthread_self 语法

pthread_t pthread_self(void);
#include <pthread.h>
pthread_t tid;
tid = pthread_self(); 

pthread_self返回值
pthread_self()返回调用线程的thread identifier。

比较线程ID

请使用pthread_equal(3C)对两个线程的线程标识号进行比较。
pthread_equal语法

int pthread_equal(pthread_t tid1, pthread_t tid2);
#include <pthread.h>
pthread_t tid1, tid2;
int ret;  

ret = pthread_equal(tid1, tid2); 

pthread_equa 返回值
如果tid1 和tid2 相等,pthread_equal() 将返回非零值,否则将返回零。如果tid1 或
tid2 是无效的线程标识号,则结果无法预测。

初始化线程

使用pthread_once(3C),可以在首次调用pthread_once时调用初始化例程。以后调用
pthread_once()将不起作用。
pthread_once语法

 int pthread_once(pthread_once_t *once_control,
void (*init_routine)(void));
#include <pthread.h>
pthread_once_t once_control = PTHREAD_ONCE_INIT;
int ret;
ret = pthread_once(&once_control, init_routine); 

once_control 参数用来确定是否已调用相关的初始化例程。

pthread_once返回值
pthread_once()在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出
现以下情况,pthread_once()将失败并返回相应的值。
EINVAL
描述: once_controlinit_routine 是NULL。

停止执行线程

使用sched_yield(3RT),可以使当前线程停止执行,以便执行另一个具有相同或更高
优先级的线程。
sched_yield 语法

 int sched_yield(void);
#include <sched.h>
int ret;
ret = sched_yield(); 

sched_yield返回值
sched_yield() 在成功完成之后返回零。否则,返回-1,并设置errno 以指示错误状
态。
ENOSYS
描述: 本实现不支持sched_yield

设置线程的优先级

请使用pthread_setschedparam(3C) 修改现有线程的优先级。此函数对于调度策略不起
作用。
pthread_setschedparam语法

int pthread_setschedparam(pthread_t tid, int policy,
    const struct sched_param *param);
#include <pthread.h>
pthread_t tid;
int ret;  

struct sched_param param;
int priority;
/* sched_priority will be the priority of the thread */
sched_param.sched_priority = priority;
policy = SCHED_OTHER;
/* scheduling parameters of target thread */
ret = pthread_setschedparam(tid, policy, &param); 

pthread_setschedparam 返回值
pthread_setschedparam()在成功完成之后返回零。其他任何返回值都表示出现了错
误。如果出现以下任一情况,pthread_setschedparam()函数将失败并返回相应的值。
EINVAL
描述: 所设置属性的值无效。
ENOTSUP
描述: 尝试将该属性设置为不受支持的值。

获取线程的优先级

pthread_getschedparam(3C)可用来获取现有线程的优先级。
pthread_getschedparam语法

int pthread_getschedparam(pthread_t tid, int policy,
    struct schedparam *param);
#include <pthread.h>
pthread_t tid; 
sched_param param;
int priority;
int policy;
int ret;
/* scheduling parameters of target thread */
ret = pthread_getschedparam (tid, &policy, &param);
/* sched_priority contains the priority of the thread */
priority = param.sched_priority; 

pthread_getschedparam返回值
pthread_getschedparam()在成功完成之后返回零。其他任何返回值都表示出现了错
误。如果出现以下情况,该函数将失败并返回对应的值。
ESRCH
描述: tid 指定的值不引用现有的线程。

向线程发送信号

请使用pthread_kill(3C) 向线程发送信号。
pthread_kill语法

int pthread_kill(thread_t tid, int sig);
#include <pthread.h>
#include <signal.h>
int sig;
pthread_t tid;
int ret;

ret = pthread_kill(tid, sig); 

pthread_kill() 将信号sig 发送到由tid 指定的线程。tid 所指定的线程必须与调用线程
在同一个进程中。sig 参数必须来自signal(5)提供的列表。
如果sig 为零,将执行错误检查,但并不实际发送信号。此错误检查可用来检查tid 的
有效性。
pthread_kill 返回值
pthread_kill()在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出
现以下任一情况,pthread_kill()将失败并返回相应的值。
EINVAL
描述: sig 是无效的信号量。
ESRCH
描述: 当前的进程中找不到tid。

访问调用线程的信号掩码

请使用pthread_sigmask(3C)更改或检查调用线程的信号掩码。
pthread_sigmask语法

int pthread_sigmask(int how, const sigset_t *new, sigset_t *old);
#include <pthread.h>
#include <signal.h>
int ret;
sigset_t old, new;

ret = pthread_sigmask(SIG_SETMASK, &new, &old); /* set new mask */
ret = pthread_sigmask(SIG_BLOCK, &new, &old); /* blocking mask */
ret = pthread_sigmask(SIG_UNBLOCK, &new, &old); /* unblocking */ 

how 用来确定如何更改信号组。how 可以为以下值之一:
SIG_BLOCK。向当前的信号掩码中添加new,其中new 表示要阻塞的信号组。
SIG_UNBLOCK。从当前的信号掩码中删除new,其中new 表示要取消阻塞的信号组。
SIG_SETMASK。将当前的信号掩码替换为new,其中new 表示新的信号掩码。
当new 的值为NULL 时,how 的值没有意义,线程的信号掩码不发生变化。要查询当前
已阻塞的信号,请将NULL 值赋给new 参数。
除非old 变量为NULL,否则old 指向用来存储以前的信号掩码的空间。

pthread_sigmask返回值
pthread_sigmask()在成功完成之后返回零。其他任何返回值都表示出现了错误。如果
出现以下情况,pthread_sigmask()将失败并返回相应的值。
EINVAL
描述: 未定义how 的值。

安全地Fork

pthread_atfork 语法

int pthread_atfork(void (*prepare) (void), void (*parent) (void),
        void (*child) (void) );

pthread_atfork返回值
pthread_atfork() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果
出现以下情况,pthread_atfork()将失败并返回相应的值。
ENOMEM
描述: 表空间不足,无法记录Fork 处理程序地址。

终止线程

请使用pthread_exit(3C)终止线程。

pthread_exit 语法

void pthread_exit(void *status);
#include <pthread.h>
void *status;
pthread_exit(status); /* exit with status */ 

pthread_exit() 函数可用来终止调用线程。将释放所有线程特定数据绑定。如果调用
线程尚未分离,则线程ID 和status 指定的退出状态将保持不变,直到应用程序调用
pthread_join()以等待该线程。否则,将忽略status。线程ID 可以立即回收。

pthread_exit 返回值
调用线程将终止,退出状态设置为status 的内容。

结束

线程可通过以下方法来终止执行:
■ 从线程的第一个(最外面的)过程返回,使线程启动例程。请参见
pthread_create
■ 调用pthread_exit(),提供退出状态。
■ 使用POSIX 取消函数执行终止操作。请参见pthread_cancel()

线程的缺省行为是拖延,直到其他线程通过”joining” 拖延线程确认其已死亡。此行为
与非分离的缺省pthread_create()属性相同,请参见pthread_detach。join 的结果是
joining 线程得到已终止线程的退出状态,已终止的线程将消失。
有一个重要的特殊情况,即当初始线程(即调用main()的线程)从main()调用返回时
或调用exit()时,整个进程及其所有的线程将终止。因此,一定要确保初始线程不会
main() 过早地返回。
请注意,如果主线程仅仅调用了pthread_exit,则仅主线程本身终止。进程及进程内
的其他线程将继续存在。所有线程都已终止时,进程也将终止。

取消线程

取消操作允许线程请求终止其所在进程中的任何其他线程。不希望或不需要对一组相
关的线程执行进一步操作时,可以选择执行取消操作。

取消线程的一个示例是异步生成取消条件,例如,用户请求关闭或退出正在运行的应
用程序。另一个示例是完成由许多线程执行的任务。其中的某个线程可能最终完成了
该任务,而其他线程还在继续运行。由于正在运行的线程此时没有任何用处,因此应
当取消这些线程。

取消点

仅当取消操作安全时才应取消线程。pthreads 标准指定了几个取消点,其中包括:
■ 通过pthread_testcancel调用以编程方式建立线程取消点。
■ 线程等待pthread_cond_waitpthread_cond_timedwait(3C)中的特定条件出现。

■ 被sigwait(2) 阻塞的线程。
■ 一些标准的库调用。通常,这些调用包括线程可基于其阻塞的函数。

缺省情况下将启用取消功能。有时,您可能希望应用程序禁用取消功能。如果禁用取
消功能,则会导致延迟所有的取消请求,直到再次启用取消请求。
有关禁用取消功能的信息,请参见“pthread_setcancelstate 语法”。

放置取消点

执行取消操作存在一定的危险。大多数危险都与完全恢复不变量和释放共享资源有
关。取消线程时一定要格外小心,否则可能会使互斥保留为锁定状态,从而导致死
锁。或者,已取消的线程可能保留已分配的内存区域,但是系统无法识别这一部分内
存,从而无法释放它。
标准C 库指定了一个取消接口用于以编程方式允许或禁止取消功能。该库定义的取消
点是一组可能会执行取消操作的点。该库还允许定义取消处理程序的范围,以确保这
些处理程序在预期的时间和位置运行。取消处理程序提供的清理服务可以将资源和状
态恢复到与起点一致的状态。
必须对应用程序有一定的了解,才能放置取消点并执行取消处理程序。互斥肯定不是
取消点,只应当在必要时使之保留尽可能短的时间。
请将异步取消区域限制在没有外部依赖性的序列,因为外部依赖性可能会产生挂起的
资源或未解决的状态条件。在从某个备用的嵌套取消状态返回时,一定要小心地恢复
取消状态。该接口提供便于进行恢复的功能:pthread_setcancelstate(3C)在所引用的
变量中保留当前的取消状态,pthread_setcanceltype(3C)以同样的方式保留当前的取
消类型。
在以下三种不同的情况下可能会执行取消操作:
■ 异步
■ 执行序列中按标准定义的各个点
■ 调用pthread_testcancel()

缺省情况下,仅在POSIX 标准可靠定义的点执行取消操作。
无论何时,都应注意资源和状态恢已复到与起点一致的状态。

取消线程

请使用pthread_cancel(3C)取消线程。
pthread_cancel语法

int pthread_cancel(pthread_t thread);
#include <pthread.h>
pthread_t thread;
int ret;
ret = pthread_cancel(thread); 

取消请求的处理方式取决于目标线程的状态。状态由以下两个函数确定
pthread_setcancelstate(3C)pthread_setcanceltype(3C)

pthread_cancel返回值
pthread_cancel()在成功完成之后返回零。其他任何返回值都表示出现了错误。如果
出现以下情况,该函数将失败并返回对应的值。
ESRCH
描述: 没有找到与给定线程ID 相对应的线程。

启用或禁用取消功能

请使用pthread_setcancelstate(3C) 启用或禁用线程取消功能。创建线程时,缺省情况
下线程取消功能处于启用状态。
pthread_setcancelstate 语法

int pthread_setcancelstate(int state, int *oldstate);
#include <pthread.h>
int oldstate;
int ret;
/* enabled */
ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
/* disabled */
ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); 

pthread_setcancelstate返回值
pthread_setcancelstate()在成功完成之后返回零。其他任何返回值都表示出现了错
误。如果出现以下情况,pthread_setcancelstate() 函数将失败并返回相应的值。
EINVAL
描述: 状态不是PTHREAD_CANCEL_ENABLE 或PTHREAD_CANCEL_DISABLE

设置取消类型

使用pthread_setcanceltype(3C)可以将取消类型设置为延迟或异步模式。
pthread_setcanceltype 语法

int pthread_setcanceltype(int type, int *oldtype);
#include <pthread.h>
int oldtype;
int ret;

/* deferred mode */
ret = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
/* async mode*/
ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); 

创建线程时,缺省情况下会将取消类型设置为延迟模式。在延迟模式下,只能在取消
点取消线程。在异步模式下,可以在执行过程中的任意一点取消线程。因此建议不使
用异步模式。

pthread_setcanceltype 返回值
pthread_setcanceltype() 在成功完成之后返回零。其他任何返回值都表示出现了错
误。如果出现以下情况,该函数将失败并返回对应的值。
EINVAL
描述: 类型不是PTHREAD_CANCEL_DEFERREDPTHREAD_CANCEL_ASYNCHRONOUS

创建取消点

请使用pthread_testcancel(3C) 为线程建立取消点。
pthread_testcancel语法

void pthread_testcancel(void);
#include <pthread.h>
pthread_testcancel(); 

当线程取消功能处于启用状态且取消类型设置为延迟模式时,pthread_testcancel()
数有效。如果在取消功能处于禁用状态下调用pthread_testcancel(),则该函数不起作
用。
请务必仅在线程取消操作安全的序列中插入pthread_testcancel()。除通过
pthread_testcancel() 调用以编程方式建立的取消点以外,pthread 标准还指定了几个
取消点。有关更多详细信息,请参见“取消点”。

pthread_testcancel返回值
pthread_testcancel() 没有返回值。

将处理程序推送到栈上

使用清理处理程序,可以将状态恢复到与起点一致的状态,其中包括清理已分配的资
源和恢复不变量。使用pthread_cleanup_push(3C)pthread_cleanup_pop(3C)函数可
以管理清理处理程序。
在程序的同一词法域中可以推送和弹出清理处理程序。推送和弹出操作应当始终匹
配,否则会生成编译器错误。
pthread_cleanup_push语法
请使用pthread_cleanup_push(3C) 将清理处理程序推送到清理栈(LIFO)。

void pthread_cleanup_push(void(*routine)(void *), void *args);
#include <pthread.h>
/* push the handler "routine" on cleanup stack */
pthread_cleanup_push (routine, arg); 

pthread_cleanup_push返回值
pthread_cleanup_push() 没有返回值。

从栈中弹出处理程序

请使用pthread_cleanup_pop(3C)从清理栈中弹出清理处理程序。
pthread_cleanup_pop 语法

void pthread_cleanup_pop(int execute);
#include <pthread.h>

/* pop the "func" out of cleanup stack and execute "func" */
pthread_cleanup_pop (1);
/* pop the "func" and DONT execute "func" */ 
pthread_cleanup_pop (0); 

如果弹出函数中的参数为非零值,则会从栈中删除该处理程序并执行该处理程序。如
果该参数为零,则会弹出该处理程序,而不执行它。
线程显式或隐式调用pthread_exit(3C)时,或线程接受取消请求时,会使用非零参数有
效地调用pthread_cleanup_pop()

pthread_cleanup_pop返回值
pthread_cleanup_pop() 没有返回值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值