linux下的多线程编程常用函数.docx_第1页
linux下的多线程编程常用函数.docx_第2页
linux下的多线程编程常用函数.docx_第3页
linux下的多线程编程常用函数.docx_第4页
linux下的多线程编程常用函数.docx_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux下pthread的实现是通过系统调用clone()来实现的。clone()是Linux所特有的系统调用,他的使用方式类似 pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg); 返回值:若是成功建立线程返回0,否则返回错误的编号形式参数: pthread_t *restrict tidp 要创建的线程的线程id指针 const pthread_attr_t *restrict attr 创建线程时的线程属性 void* (start_rtn)(void) 返回值是void类型的指针函数 void *restrict arg start_rtn的行参进行编译的时候要加上-lpthread向线程传递参数。例程2: 功能:向新的线程传递整形值#include #include #include void *create(void *arg) int *num; num=(int *)arg; printf(create parameter is %d n,*num); return (void *)0;int main(int argc ,char *argv) pthread_t tidp; int error; int test=4; int *attr=&test; error=pthread_create(&tidp,NULL,create,(void *)attr); if(error) printf(pthread_create is created is not created . n); return -1; sleep(1); printf(pthread_create is created .n); return 0; 编译方法:gcc -lpthread pthread_int.c -Wall 执行结果:create parameter is 4pthread_create is created is created . 例程总结: 能够看出来,我们在main函数中传递的整行指针,传递到我们新建的线程函数中。 在上面的例子能够看出来我们向新的线程传入了另一个线程的int数据,线程之间还能够传递字符串或是更复杂的数据结构。例程3: 程式功能:向新建的线程传递字符串 程式名称:pthread_string.c #include #include #include void *create(void *arg) char *name; name=(char *)arg; printf(The parameter passed from main function is %sn,name); return (void *)0;int main(int argc, char *argv) char *a=zieckey; int error; pthread_t tidp; error=pthread_create(&tidp, NULL, create, (void *)a); if(error!=0) printf(pthread is not created.n); return -1; sleep(1); printf(pthread is created. n); return 0; 程式目的:验证新建立的线程能够共享进程中的数据 程式名称:pthread_share.c#include #include #include static int a=4;void *create(void *arg) printf(new pthread . n); printf(a=%dn,a); return (void *)0;int main(int argc,char *argv) pthread_t tidp; int error; a=5; error=pthread_create(&tidp, NULL, create, NULL); if(error!=0) printf(new thread is not create . n); return -1; sleep(1); printf(new thread is created . n); return 0;2、线程的终止 假如进程中任何一个线程中调用exit,_Exit,或是_exit,那么整个进程就会终止, 和此类似,假如信号的默认的动作是终止进程,那么,把该信号发送到线程会终止进程。 线程的正常退出的方式: (1) 线程只是从启动例程中返回,返回值是线程中的退出码 (2) 线程能够被另一个进程进行终止 (3) 线程自己调用pthread_exit函数 两个重要的函数原型:#includevoid pthread_exit(void *rval_ptr); /*rval_ptr 线程退出返回的指针*/int pthread_join(pthread_t thread,void *rval_ptr); /*成功结束进程为0,否则为错误编码*/ 例程6 程式目的:线程正常退出,接受线程退出的返回码 程式名称:pthread_exit.c#include#include#includevoid *create(void *arg) printf(new thread is created . n); return (void *)8;int main(int argc,char *argv) pthread_t tid; int error; void *temp; error = pthread_create(&tid, NULL, create, NULL); if( error ) printf(thread is not created . n); return -1; error = pthread_join(tid, &temp); if( error ) printf(thread is not exit . n); return -2; printf(thread is exit code %d n, (int )temp); return 0; 线程退出能够返回线程的int数值。线程退出不但仅能够返回线程的int数值,还能够返回一个复杂的数据结构。 例程7 程式目的:线程结束返回一个复杂的数据结构 程式名称:pthread_return_struct.c#include#include#includestruct menber int a; char *b;temp=8,zieckey;void *create(void *arg) printf(new thread . n); return (void *)&temp;int main(int argc,char *argv) int error; pthread_t tid; struct menber *c; error = pthread_create(&tid, NULL, create, NULL); if( error ) printf(new thread is not created . n); return -1; printf(main . n); error = pthread_join(tid,(void *)&c); if( error ) printf(new thread is not exit . n); return -2; printf(c-a = %dn,c-a); printf(c-b = %sn,c-b); sleep(1); return 0;3、线程标识 函数原型: pthread_t pthread_self(void);pid_t getpid(void);getpid()用来取得现在进程的进程识别码,函数说明实现在新建立的线程中打印该线程的id和进程id 程式名称:pthread_id.c void *create(void *arg) printf(New thread . n); printf(This threads id is %un, (unsigned int)pthread_self(); printf(The process pid is %dn,getpid(); return (void *)0; int main(int argc,char *argv) pthread_t tid; int error; printf(Main thre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论