LINUX线程函数大全doc.docx
- 文档编号:11389283
- 上传时间:2023-02-28
- 格式:DOCX
- 页数:15
- 大小:20.86KB
LINUX线程函数大全doc.docx
《LINUX线程函数大全doc.docx》由会员分享,可在线阅读,更多相关《LINUX线程函数大全doc.docx(15页珍藏版)》请在冰豆网上搜索。
LINUX线程函数大全doc
LINUX线程函数大全
线程
创建一个缺省的线程
缺省的线程的属性:
1非绑定
1未分离
1一个缺省大小的堆栈
1具有和父线程一样的优先级
用phread_attr_init()创建一个缺省的属性对象,
用属性对象创建一个线程pthread_create(3T)
intpthread_create(pthread_t*tid,constpthread_attr_t*tattr,void*(*start_routine)(void*),void*arg);
#include
pthread_attr_ttattr;
pthread_ttid;
externvoid*start_routine(void*arg);
void*arg;
intret;
/*defaultbehavior*/
ret=pthread_create(&tid,NULL,start_routine,arg);
/*initwithdefaultattributes*/
/^defaultbehaviorspecified*/
ret=pthread_create(&tid,&tagstart_routine,arg);
tattr中含有初始化线程所需的属性,值赋为NULL即缺省。
start.routine是线程入口函数的起始地址。
当start_routine返回时,相应的线程就结朿了。
线程结朿时的退出状态值是start_routine函数用phread_exit()函数返冋的返冋值。
当pthread_create()函数调用成功时,线程标识符保存在参数tid指针指向屮。
返回值,pthread_create()成功后返回0,
EAGAIN超过了某个限制,如LWPs过多。
EINVALtattr值非法。
创建子线程时,传给子线程的输入参数最好是malloc()返回的指针(这样的指针指向进程堆中的存储空间)或指向全局变量的指针,而不要是指向局部变量的指针。
因为当子线程访问输入参数时,创建子线程的函数可能已结束,局部变量也就不存在了。
等待线程结束
pthread」oin(3T)
intpthread_join(pthread_ttid,void**statu$);
#include
pthread_ttid;
intret;
intstatus;
/*wailingtojointhread"lid"withstatus*/
ret=pthreadJoin(tid,&status);
/*waitingtojointhread"tid"withoutstatus*/
ret=pthreadjoin(tid,NULL);
pthreadjoin()会阻塞调用它的线程,直到参数tid指定的线程结束。
tid指定的线程必须在当前进程屮,且必须是非分离的。
status接收指定线程终止时的返回状态码。
不能有多个线程等待同一个线程终止,否则返回错误码ESRCH
当pthreadjoin()返回时,终止线程占用的堆栈等资源已被回收。
返回值:
成功返回0
ESRCHtid指定的线程不是一个当前线程中合法且未分离的线程。
EDEADLKtid指定的是当前线程。
E1NVALtid非法。
分离一个线程
pthread_detach(3T)
将非分离线程设置为分离线程。
intpthread_detach(pthread_ttid);
#include
pthread_ttid;
intret;
ret=pthread_detach(tid);
该函数通知线程库,当线程终止以后,tid所指线程的内存可以被立即收冋。
返冋值:
成功返回0
EINVALtid所指线程不是一个合法线程。
ESRCHtid指定的线稈不是一个当前线程中合法且未分离的线程。
为线程数据创建一个键
多线程的c语言程序具有三种数据:
局部变量,全局变量,线程数据(TSD)
TSD类似于全局变量,但是线程私有的。
每个TSD都有个键同他相关联。
intpthread_key_create(pthread_key_t*key,void(*destructor)(*void));
#include
pthread_key_tkey;
intret;
/*keycreatewithoutdestructor*/
ret=pthread_key_create(&key,NULL);
/*keycreatewithdestructor*/
ret=pthread_key_destructor(&key,destructor);
key指向的内存区有效。
该函数成功时,份配的建放在key中,必须保证destructor用来释放不再需要的内存。
返回值:
成功返回0
EAGAINkey名字空间耗尽
ENOMEM没有足够的内存空间创建一个新的键。
删除线程数据的键
pthread_key_delete(3T)
Solaris线程接口中没有该函数
intpthread_key_delete(pthread_key_tkey);
#include
pthread_key_tkey;
intret;
ret=pthread_key_delete(key);
在调用该函数之前,必须释放和本线程相关联的资源,发键的解析函数。
EINVALkey值非法
设置线程数据键
pthread_setspecific(3T)
设置和某个线程数据键绑定在一起的线程数据(一般是指针)函数将指向专有数据的指针value设置进由key指示的结构体中;
intpthread_setspecific(pthread_key_tkey,constvoid*value);
#include
pthread_key_tkey;
void*value;
intret;
ret=pthread_setspecific(key,value);
返回值:
成功返回()
ENOMEM没有足够的虚拟内存
EINVALkey值非法
plhread_setspecific()不释放原来绑定在键上的内存,给一个键绑定新的指针时,必须释放原指针指向的内存,否则会发生内存泄漏。
获取线程数据键
pthread_getspecific(3T)
获取绑定在线程数据键上的值,并在指定的位置存储值
返回存放在对应结构体屮的专有指针;
intpthread_getspecific(pthread_key_tkey,void**value)pthread_key_tkey;
void*value;
pthread_getspecific(key,&value);
返冋值:
不返回错误码
取线程标识符
pthread_self(3T)
取当前线程的标识符
pthread_tpthread_self(void);
#include
pthread_ttid;
tid=pthread_self();
返冋值:
当前线程标识符。
比较线程标识符
pthread_equal(3T)
intpthread_equal(pthread_ttidl,pthread_ttid2);
#include
pthread_ttidl,tid2
intret;
ret=pthread_equal(tidl,tid2);
返回值:
如果tidl和tid2相同返回非零值,否则返回0。
如果参数非法,返回值不可预知。
初始化线程
用來调用初始化函数,只有第一次调用有效。
intpthread_once(pthread_once_t*once_control,void(*init_routine)(void));
#include
pthread_once_tonce_control=PTHREAD_ONCE_INIT;
intret;
ret=pthread_once(&once_control,init_routine);
once_control界定了相应的初始化函数是否被调用过。
返回值:
成功返回0
EINVAL某个参数为NULL
出让当前线程对处理器的控制权
sched_yeild(3R)
把当前线程的优先权让给有相同或更高优先级的线程。
intsched_yeild(void);
#include
intret;
ret=sched_yeild();
返回值:
成功返回()
ENOSYS当前版本不支持sched_yield()
设置线程的优先级
pthread_setschedparam(3T)intpthread_setschedparam(pthread_ttid,intpolicy,conststructsched_param
*param);
pthread_ttid;
intret;
structsched^paramparam;
intpriority;
/*sched_prioritywillbethepriorityofthethread*/
sched_param,sched_priority=priority;
/*onlysupportedpolicy,otherwillresultinENOTSUP*/
policy=SCHED_OTHER;
^schedulingparametersoftargetthread*/
ret=pthread_setschedparam(tid,policy,¶m);
返冋值:
成功返回0
EINVAL属性值非法
ENOTSUP属性值在当前版本不支持
取线程的优先级
pthread_getschedparam(3T)
intpthread_getschedparam(pthread_ttid,intpolicy,structschedparam*param);
#include
pthread_ttid;
sched_paramparam;
intprioprity;
intpolicy;
intret;
/^schedulingparametersoftargetthread*/
ret=pthread_getschedparam(tid,&policy,¶m);
/*sched_prioritycontainsthepriorityofthethread*/
priority=param.sched_priority;
返回值:
成功返回0
ESRCHtid不是一个现存的线程。
向线程发信号
pthread_kill(3T)
intpthread_kill(pthread_ttid,intsig);
#include
#include
intsig;
pthread_ttid;
intret;
ret=pthread_kill(tid,sig);
tid指定的线程必须和函数当前线程在同一个进程中。
sig为0时,进行错误检查,不发送信号,往往被用来检查tid的合法性。
返冋值:
成功返回0
EINVALsig不是合法信号量
ESRCHtid不是当前进程中的线程
访问当前线程的信号掩码
pthread_sigmask(3T)
intpthread_siginask(inthow,constsigset_t*new,sigset_t*old);
#include
#include
intret;
sigset_told,new;
ret=pthread_sigmask(SIG_SETMASK,&new,&old);
ret=pthread_sigmask(SIG_BLOCK,&new,&ol(l);
ret=pthread_sigmask(SIG_UNBLOCK,&new,&old);
how表示对当前信号掩码进行什么操作。
SIG_SETMASK:
在信号掩码中加入new信号集,new表示新增加的要屏蔽的信号。
SIG.BLOCK:
在信号掩码中删去new信号集,new表示新增加的不需再屏蔽的信号。
SIG_UNBLOCK:
用new信号集替换信号掩码,new表示所有需要屏蔽的信号。
当new为NULL时,无论how是什么,当前线程的信号掩码都不会改变。
旧的信号掩码保存在old中。
返回值:
成功返回0
EINVALhow的值未定义
安全的复制
pthread_atfork(3T)intpthread_atfork(void(*prepare)(void),void(*parent)(void),void(*child)(void));
终止线程
pthread_exit(3T)
voidpthread_exit(void*status);
#include
intstatus;
pthread_exit(&status);
终止当前线程,所有绑定在线程键上的内存将释放。
如果当前线程是未分离的,该线程的标识符和推出代码(status)被保留,直到其它线程用pthread」oin()等待当前线程的终止。
如果当前线程是分离的,status被忽略,线程标识符立即收回。
返回值:
若status不为NULL,线程的退出代码被置为status指向的值。
一个线程可以用一下方式终止自身运行。
从线程的入口函数返冋。
调用pthread_exit()
用POSIX的pthread_cancel()
退出可以用以下方式:
异步的
由线程库定义的一系列退出点
有应用程序定义的一系列退出点
退出点
由程序通过pthread_testcancel()建立
调用了pthread_cond_wait()或pthread_cond_timedwait()等待一个条件的线程
调用了pthreadjoin()等待另一个线程结束的线程。
被阻塞在sigwait
(2)上的线程。
退出线程
pthread_cancel(3T)
intpthread_cancel(pthread_tthread);
#include
plhread_tthread;
intret;
ret=pthread_cancel(thread);
返回值:
成功返回0
ESRCH无指定的线程。
允许或禁止退出
pthread_setcancelstate(3T)
缺省是允许退出的。
intpthread_setcancelstate(intstate,int*oldstate);
#include
intoldstate;
intret;
/*enable*/
ret=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);
/^disabled*/
ret=pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);
返冋值:
成功返冋0
EINVALstate值非法
设置退出类型
pthread_setcanceltype(3T)
可以设置成延迟类型或异步类型。
缺省是延迟类型。
异步类型下,线程可以在执行中的任何时候被退出。
intpthread_setcanceltype(inttype,int*oldtype);
#include
intoldtype;
intret;
/^deferredmode*/
ret=pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);
/*asyncmode*/
ret=pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldtype);
返冋值:
成功返冋0
EINVALstate值非法
创建退出点
pthread_testcancel(3T)
voidpthread_testcancel(void);
#include
pthread_testcancel();
只有当线程的退岀状态是允许退岀,退岀类型是延迟类型时,有效。
没有返回值。
将一个善后处理函数推入退出堆栈
pthread_cleanup_push(3T)
pthread_cleanup_pop(3T)
voidPthread_cleanup_push(void(*routine)(void*),void*args);
voidpthread_cleanup_pop(intexecute);
#include
/*pushthehandler"routine^oncleanupstack*/pthread_cleanup_push(routine,arg);
/*popthe"func',outofcleanupstackandexecute"func"*/pthread_cleanup_pop
(1);
/*popthe“func"anddon?
texecute“func^*/pthread_cleanup_pop(0);
1.线程属性
只能在线程创建时制定属性,不能在运行时改变它。
一旦属性对象被配置好,它在进程范围内都是有效的。
初始化属性
pthread_attr_init(3T)
初始化一个线程屈性对彖,屈性值是缺省值,占用内存由线程库分配。
intpthread_attr_init(pthread_attr_t*tattr);
#include
/*initializeanattributetothedefaultvalue*/ret=pthread_attr_init(&tattr);
属性对象的缺省值:
scope(线程域)
PTHREAD_SCOPE_PROCESS
Detachstate(分离状态)
PTHREAD_CREATE_JOINABLE
Stackaddr(堆栈地址)
NULL
Stacksize(堆栈大小)
1Mb
priority(优先级)父进程优先级
Inheritsched(继承调度优先级)PTHREAD_INHERIT_SCHED
schedpolicy(调度策略)
SCHED_OTHER
返回值:
成功返回0
ENOMEM没有足够的内存初始化线程属性对象
释放属性对象
pthread_attr_destroy(3T)
intpthread_attr_destroy(pthread_attr_t*tattr);
#include
pthread_attr_ttattr;
intret;
ret=pthread_attr_destroy(&tattr);
返冋值:
成功返回0
设置分离状态
pthread_attr.setdetachstate(3T)
创建线程吋,如果指定这个线程为分离线程,一旦这个线程终止,他的线程标识符和其他相关的资源可以立即被使用。
如果不需要等待某个线程终止,可以把它设定为分离。
intpthread_attr_setdetachstate(pthread_attr_t*tattr,intdetachstate);
#include
pthread_attr_ttattr;
/*setthethreaddetachstate*/
ret=pthread_attr_setdetachstate(&lagPTHREAD_CREATE_DETACHED);
返回值:
成功返回0
EINVALtattr的值或detachstate的值非法
取分离状态
pthread_attr_getdetachstate(3T)
intpthread_attr_getdetachstate(constpthread_attr_t*tattr,int*detachstate);
#include
pthread_attr_ttattr;
intdetachstate;
intret;
ret=pthread_attr_getdetachstate(&tattr,&detachstate);
返回值:
成功返回0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LINUX 线程 函数 大全 doc
![提示](https://static.bdocx.com/images/bang_tan.gif)