当前位置:文档之家› 线程基本编程——线程函数大全

线程基本编程——线程函数大全

线程基本编程——线程函数大全
线程基本编程——线程函数大全

线程基本编程

索引:

1.创建线程pthread_create

2.等待线程结束pthread_join

3.分离线程pthread_detach

4.创建线程键pthread_key_create

5.删除线程键pthread_key_delete

6.设置线程数据pthread_setspecific

7.获取线程数据pthread_getspecific

8.获取线程标示符pthread_self

9.比较线程pthread_equal

10.一次执行pthread_once

11.出让执行权sched_yield

12.修改优先级pthread_setschedparam

13.获取优先级pthread_getschedparam

14.发送信号pthread_kill

15.设置线程掩码pthread_sigmask

16.终止线程pthread_exit

17.退出线程pthread_cancel

18.允许/禁止退出线程pthread_setcancelstate

19.设置退出类型pthread_setcanceltype

20.创建退出点pthread_testcancel

21.压入善后处理函数pthread_cleanup_push

22.弹出善后处理函数pthread_cleanup_pop

--------------------------------------------------------------------------------

1.创建线程pthread_create

#include

int pthread_create(pthread_t *tid, const pthread_attr_t *tattr, void *(*start_routine)(void *), void *arg);

返回值:函数成功返回0。任何其他返回值都表示错误。

创建一个线程。

参数tattr中含有初始化线程所需要的属性,start_routine是线程入口函数的地址,当start_routine返回时,相应的线程就结束了。

当函数成功时,线程标示符保存在参数tid指向的内存中。

如果不指定属性对象,将其置为NULL,则创建一个缺省的线程,有如下属性:

非绑定的;

未分离的;

由一个缺省大小的堆栈;

具有和父线程一样的优先级。

注意:在创建子线程时,传给子线程的输入参数最好是由malloc()函数返回的指针或指向全局变量的指针,而不要是指向局部变量的指针。要保证子线程处理参数时,该区域仍然有效。--------------------------------------------------------------------------------

2.等待线程结束pthread_join

#include

int pthread_join(pthread_t tid, void **status);

返回值:函数成功返回0。任何其他返回值都表示错误。

等待一个线程结束。

该函数阻塞调用它线程,直到参数tid指定的线程结束。

tid指定的线程必须在当前进程中,同时tid指定的线程必须是非分离的。

不能有多个线程等待同一个线程终止。如果出现这种情况,一个线程将成功返回,别的线程将返回错误ESRCH。

如果参数status不为NULL,则将线程的退出状态放在status指向的内存中。

--------------------------------------------------------------------------------

3.分离线程pthread_detach

#include

int pthread_detach(pthread_t tid);

返回值:函数成功返回0。任何其他返回值都表示错误。

将非分离的线程设置为分离线程。即通知线程库在指定的线程终止时回收线程占用的内存等资源。

在一个线程上使用多次pthread_detach的结果是不可预见的。

--------------------------------------------------------------------------------

4.创建线程键pthread_key_create

#include

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));

返回值:函数成功返回0。任何其他返回值都表示错误。

在进程中分配一个键值,这个键被用来表示一个线程数据项。这个键对进程中所有的线程都是可见的。刚创建线程数据键时,在所有线程中和这个键相关联的值都是NULL。

函数成功返回后,分配的键放在key参数指向的内存中,必须保证key参数指向的内存区的有效性。

如果指定了解析函数destructor,那么当线程结束时并且将非空的值绑定在这个键上,系统将调用destructor函数,参数就是相关线程与这个键绑定的值。绑定在这个键上的内存块可由destructor函数释放。

--------------------------------------------------------------------------------

5.删除线程键pthread_key_delete

#include

int pthread_key_delete(pthread_key_t key);

返回值:函数成功返回0。任何其他返回值都表示错误。

删除线程数据键。这个键占用的内存将被释放,该键再被引用将返回错误。

在调用该函数之前,程序必须释放和本线程相关联的资源,该函数不会引发线程数据键的解析函数。

--------------------------------------------------------------------------------

6.设置线程数据pthread_setspecific

#include

int pthread_setspecific(pthread_key_t key, const void *value);

返回值:函数成功返回0。任何其他返回值都表示错误。

设置和某个线程数据键绑定在一起的线程专用数据(一般是指针)。

函数不会释放原来绑定在键上的内存,给一个键值绑定新的指针时,必须释放原指针指向的

内存,否则会发生内存泄漏。

--------------------------------------------------------------------------------

7.获取线程数据pthread_getspecific

#include

void pthread_getspecific(pthread_key_t key, void **value);

无返回值。出错时value指向NULL。

获取绑定在线程数据键上的值,并在指定的位置存储取来的值。

--------------------------------------------------------------------------------

8.获取线程标示符pthread_self

#include

pthread_t pthread_self(void);

返回当前线程的标示符。

--------------------------------------------------------------------------------

9.比较线程pthread_equal

#include

int pthread_equal(pthread_t tid1, pthread_t tid2);

如果tid1和tid2相同,函数返回一个非0值,否则返回0。

如果tid1或tid2中任何一个是非法值,则返回将是不可预料的。

--------------------------------------------------------------------------------

10.一次执行pthread_once

#include

int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));

返回值:函数成功返回0。任何其他返回值都表示错误。

函数用来调用初始化函数。如果已经有线程通过pthread_once调用过这个初始化函数一次,那么以后通过pthread_once函数再调用这个初始化函数将无效。

参数once_control决定了相应的初始化函数是否被调用过。它一般如下使用:

[static] pthread_once_t once_control = PTHREAD_ONCE_INIT。

--------------------------------------------------------------------------------

11.出让执行权sched_yield

#include

int sched_yield(void);

返回值:函数成功返回0。-1表示错误。

把当前线程的执行权(即对处理器的控制权)出让给另一个有相同或更高优先级的线程。--------------------------------------------------------------------------------

12.修改优先级pthread_setschedparam

#include

int pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param);

返回值:函数成功返回0。任何其他返回值都表示错误。

修改线程的优先权。

--------------------------------------------------------------------------------

13.获取优先级pthread_getschedparam

#include

int pthread_getschedparam(pthread_t tid, int policy, struct schedparam *param);

返回值:函数成功返回0。任何其他返回值都表示错误。

获取线程的优先级。

--------------------------------------------------------------------------------

14.发送信号pthread_kill

#include

int pthread_kill(pthread_t tid, int sig);

返回值:函数成功返回0。任何其他返回值都表示错误。

向tid指定的线程发送一个信号,tid指定的线程必须和当前线程在同一个进程中。

当sig参数为0时,函数将进行错误检查,不发送信号,这常常用来检查tid的合法性。

--------------------------------------------------------------------------------

15.设置线程掩码pthread_sigmask

#include

#include

int pthread_sigmask(int how, const sigset_t *new, sigset_t *old);

返回值:函数成功返回0。任何其他返回值都表示错误。

改变或检验当前线程的信号掩码。

参数how表示对当前信号掩码进行什么操作,有如下值:SIG_BLOCK、SIG_UNBLOCK、SIG_SETMASK。

当参数new为NULL时,不论how的值是什么,当前线程的信号掩码都不会改变。

旧的信号掩码保存在参数old指向的内存中,当old不为NULL时。

--------------------------------------------------------------------------------

16.终止线程pthread_exit

#include

void pthread_exit(void *status);

终止当前线程,所有绑定在线程数据键上的内存将被释放。如果当前线程是非分离的,那么这个线程的标示符合退出代码将被保留,直到其他线程用pthread_join来等待当前线程的终止。如果当前线程是分离的,status将被忽略,线程标示符将被立即回收。

若status不为NULL,线程的退出代码被置为status参数指向的值。

--------------------------------------------------------------------------------

17.退出线程pthread_cancel

#include

int pthread_cancel(pthread_t thread);

返回值:函数成功返回0。任何其他返回值都表示错误。

退出一个线程。如何响应退出请求取决于目标线程的状态。

--------------------------------------------------------------------------------

18.允许/禁止退出线程pthread_setcancelstate

#include

int pthread_setcancelstate(int state, int *oldstate);

返回值:函数成功返回0。任何其他返回值都表示错误。

参数state取值为PTHREAD_CANCEL_ENABLE或PTHREAD_CANCEL_DISABLE。

--------------------------------------------------------------------------------

19.设置退出类型pthread_setcanceltype

#include

int pthread_setcanceltype(int type, int *oldtype);

返回值:函数成功返回0。任何其他返回值都表示错误。

将线程退出类型设置为延迟类型或异步类型。参数type的取值为PTHREAD_CANCEL_DEFERRED或PTHREAD_CANCEL_ASYNCHRONOUS。

当一个线程被创建后,缺省值是延迟类型。在异步方式下,线程可以在执行的任何时候被退出。

--------------------------------------------------------------------------------

20.创建退出点pthread_testcancel

#include

void pthread_testcancel(void);

无返回值。

设置线程的退出点。

只有当线程的退出状态是允许退出的,而且线程的退出类型是延迟时,调用该函数才有效。如果调用时线程的退出状态是禁止的,则该调用不起作用。

小心使用该函数,只有在能够安全的被退出的地方才能够设置退出点。

--------------------------------------------------------------------------------

21.压入善后处理函数

#include

void pthread_cleanup_push(void (*routine)(void *), void *args);

将一个善后处理函数压入善后处理函数堆栈。

--------------------------------------------------------------------------------

22.弹出善后处理函数

#include

void pthread_cleanup_pop(int execute);

从善后处理函数堆栈中弹出一个善后处理函数。如果参数execute非0,则执行弹出的函数;如果参数为0,则不执行弹出函数。

如果一个线程显式或隐式的调用pthread_exit()函数或线程接受了退出请求,线程库实际上将会以非0参数调用pthread_cleanup_pop函数。

本文来自ChinaUnix博客,如果查看原文请点:https://www.doczj.com/doc/dc767409.html,/u1/46715/showart_1225657.html

相关主题
文本预览
相关文档 最新文档