linux多线程编程_第1页
linux多线程编程_第2页
linux多线程编程_第3页
linux多线程编程_第4页
linux多线程编程_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

Linux 多线程编程的基本的函数多线程编程的基本的函数 1 1 线程创建线程创建 函数原型 include int pthread create pthread t restrict tidp const pthread attr t r estrict 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 类型的指针函数 vodi restrict arg start rtn 的行参 例题 1 功能 测试建立一个新的线程 程序名称 pthread test c include include void create void arg printf new thread created int main int argc char argv pthread t tidp int error error pthread create if error 0 printf pthread create is not created return 1 printf prthread create is created return 0 编译方法 gcc Wall lpthread pthread test c 因为 pthread 的库不是 linux 系统的库 所以在进行编译的时候要加上 lpthread 否则编译不过 会出现下面错误 thread test c 在函数 create 中 thread test c 7 警告 在有返回值的函数中 程序流程到达函数尾 tmp ccOBJmuD o In function main thread test c text 0 x4f 对 pthread create 未定义的引用 collect2 ld 返回 1 现在我们能建立了一个线程了 我们可以从函数的原型看到 在创建线程的时 候 是可以在对我们的函数进行传递参数 在 pthread create 的第四个行参 我们看一下例题 2 3 例题 2 功能 向新的线程传递整形值 程序名称 pthread int c include include include void create void arg int num num int arg printf create parameter is d num return void 0 int main int argc char argv pthread t tidp int error int test 4 int attr error pthread create if error 0 printf pthread create is created is not created return 1 sleep 1 printf pthread create is created is created return 0 编译方法 gcc lpthread thread int c Wall 执行结果 create parameter is 4 pthread create is created is created 例题总结 可以看出来 我们在 main 函数中传递的整行指针 传递到我们新建的线程 函数中 在上面的例子可以看出来我们向新的线程传入了另一个线程的 int 数据 线程之间还可以传递字符串或是更复杂的数据结构 例题 3 程序功能 向新建的线程传递字符串 程序名称 thread char c include include include void create void arg char name name char arg printf arg is s name return void 0 int main int argc char argv char a wang int error pthread t tidp error pthread create if error 0 printf pthread is not created return 1 sleep 1 printf pthread is created return 0 编译方法 gcc Wall thread char c lpthread 执行结果 arg is wang pthread is created 例题总结 可以看出来 main 函数中的字符串传入了新建里的线程中 例题 4 程序名称 thread struct c include include include include malloc struct test int a char s void create void arg struct test temp temp struct test arg printf test a d temp a printf test s s temp s return void 0 int main int argc char argv pthread t tidp int error struct test b b struct test malloc sizeof struct test b a 4 b s wang error pthread create if error 0 printf phread is not created return 1 sleep 1 printf pthread is created return 0 编译方法 gcc Wall lpthread thread struct c 执行结果 test a 4 test s wang pthread is created 线程包含了标识进程内执行环境必须的信息 他集成了进程中的所有信息都是 对线程进行共享的 包括文本程序 程序的全局内存和堆内存 栈以及文件描 述符 例题 5 程序目的 验证新建立的线程可以共享进程中的数据 程序名称 thread share c include include include static int a 4 void create void arg printf new pthread printf a d a return void 0 int main int argc char argv pthread t tidp int error a 5 error pthread create if error 0 printf new thread is not create return 1 sleep 1 printf new thread is created return 0 编译方法 gcc Wall lpthread thread share c 执行结果 new pthread a 5 new thread is created 例题总结 可以看出来 我们在主线程更改了我们的全局变量 a 的值的时 候 我们新建立的线程则打印出来了改变的值 可以看出可以访问线程所在进 程中的数据信息 2 2 线程的终止 线程的终止 如果进程中任何一个线程中调用 exit Exit 或者是 exit 那么整个进 程就会终止 与此类似 如果信号的默认的动作是终止进程 那么 把该信号 发送到线程会终止进程 线程的正常退出的方式 1 线程只是从启动例程中返回 返回值是线程中的退出码 2 线程可以被另一个进程进行终止 3 线程自己调用 pthread exit 函数 两个重要的函数原型 include void pthread exit void rval ptr rval ptr 线程退出返回的指针 int pthread join pthread t thread void rval ptr 成功结束进程为 0 否则为错误编码 例题 6 程序目的 线程正常退出 接受线程退出的返回码 程序名称 exit return c include include include void create void arg printf new thread is created return void 2 int main int argc char argv pthread t tid int error void temp error pthread create if error 0 printf thread is not created return 1 error pthread join tid if error 0 printf thread is not exit return 2 printf thread is exit code d int temp sleep 1 printf thread is created return 0 编译方法 gcc Wall exit return c lpthread 执行结果 new thread is created thread is exit code 2 thread is created 线程退出不仅仅可以返回线程的 int 数值 还可以返回一个复杂的数据结 构 例题 7 程序目的 线程结束返回一个复杂的数据结构 程序名称 exit thread c include include include struct test int a char b struct test temp a 4 b wang void create void arg printf new thread return void int main int argc char argv int error pthread t tid struct test c error pthread create if error 0 printf new thread is not created return 1 printf main error pthread join tid void if error 0 printf new thread is not exit return 2 printf c a d c a printf c b s c b sleep 1 return 0 编译方法 gcc Wall lpthread exit struct c 执行结果 main new thread c a 4 c b wang 例题总结 一定要记得返回的数据结构要是在这个数据要返回的结构没有 释放的时候应用 如果数据结构已经发生变化 那返回的就不会是我们所需要 的 而是藏数据阿 3 3 线程标识 线程标识 函数原型 include pthread t pthread self void 例题 8 程序目的 实现在新建立的线程中打印该线程的 id 和进程 id 程序名称 thread self c include include include getpid void create void arg printf new thread printf thread tid u unsigned int pthread self printf thread pid is d getpid return void 0 int main int argc char argv pthread t tid int error printf Main thread is starting error pthread create if error 0 printf thread is not created return 1 printf main pid is d getpid sleep 1 return 0 编译方法 gcc Wall lpthread thread self c 执行结果 Main thread is starting main pid is 6860 new thread thread tid 3084954544 thread pid is 6860 4 4 线程的处理程序线程的处理程序 函数原型 include void pthread cleanup push void rtn void void arg 函数 rtn 是清理函数 arg 是调用参数 void pthread cleanup pop int execute 在前面讲过线程的终止方式 是正常终止还是非正常终止 都会存在一个 资源释放的问题 在 posix 中提供了一组 就是我们上面看的函数进行线程退 出的处理函数 有些像在进程中的 atexit 函数 释放的方式是指 pthread cleanup push 的调用点到 pthread cleanup pop 之间程序段进行终止 pthread cleanup push pthread cleanup pop 采用先入后出的方式的栈的管 理方式 void rtn void 在执行 pthread cleanup push 时压入函数栈 多次执行 pthread cleanup push 形成一个函数链 在执行这个函数链的时候 会以反方向弹出 即先入后出 execute 参数表识 是否执行弹出清理函数 当 execute 0 时不进行弹出清理函数 非零的时候弹出处理函数 例题 9 程序目的 实现在正常结束线程的时候 进行函数处理 程序名称 thread clean c 编译方法 执行结果 thread 1 start thread 1 push complete thread 1 exit code 1 thread 2 start thread 2 push complete cleanup thread 2 second handler cleanup thread 2 first handler thread 2 exit code 2 gcc Wall lpthread thread clean c include include include void clean void arg printf cleanup s char arg return void 0 void thr fn1 void arg printf thread 1 start pthread cleanup push clean thread 1 first handler pthread cleanup push clean thread 1 second hadler printf thread 1 push complete if arg return void 1

温馨提示

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

评论

0/150

提交评论