1 pthread_create()函数

创建线程

A:依赖的头文件

#include<pthread.h>

B:函数声明

int pthread_create(pthread_t *thread, constpthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

pthread_t *thread:传递一个pthread_t变量地址进来,用于保存新线程的tid(线程ID)

const pthread_attr_t *attr:线程属性设置,如使用默认属性,则传NULL

void *(*start_routine) (void *):函数指针,指向新线程应该加载执行的函数模块

void *arg:指定线程将要加载调用的那个函数的参数

返回值:成功返回0,失败返回错误号。以前学过的系统函数都是成功返回0,失败返回-1,而错误号保存在全局变

量errno中,而pthread库的函数都是通过返回值返回错误号,虽然每个线程也都有一个errno,但这是为了兼容其

它函数接口而提供的,pthread库本身并不使用它,通过返回值返回错误码更加清晰。

在gcc编译链接的时候,后面要加上-lpthread

关于pthread_t

typedef unsigned long int pthread_t;(32位平台下)

在一个线程中调用pthread_create()创建新的线程后,当前线程从pthread_create()

返回继续往下执行,而新的线程所执行的代码由我们传给pthread_create的函数指针

start_routine决定。start_routine函数接收一个参数,是通过pthread_create的arg参

数传递给它的,该参数的类型为void *,这个指针按什么类型解释由调用者自己定

义。start_routine的返回值类型也是void *,这个指针的含义同样由调用者自己定义。start_routine返回时,这个线程就退出了,其它线程可以调用pthread_join得到start_routine的返回值,类似于父进程调用wait(2)得到子进程的退出状态,稍后详细介绍pthread_join。

pthread_create成功返回后,新创建的线程的id被填写到thread参数所指向的内存单

元。我们知道进程id的类型是pid_t,每个进程的id在整个系统中是唯一的,调用getpid(2)

可以获得当前进程的id,是一个正整数值。线程id的类型是thread_t,它只在当前进程中

保证是唯一的,在不同的系统中thread_t这个类型有不同的实现,它可能是一个整数值,

也可能是一个结构体,也可能是一个地址,所以不能简单地当成整数用printf打印,调用

pthread_self(3)可以获得当前线程的id。

attr参数表示线程属性,本节不深入讨论线程属性,所有代码例子都传NULL给attr参

数,表示线程属性取缺省值,感兴趣的读者可以参考[APUE2e]。

C案例说明(程序编写,测试最大能够创建的线程数)

执行下面命令:

运行./app,结果如下:

2 pthread_self()函数

获取调用线程tid

A依赖的头文件

#include<pthread.h>

B函数声明

pthread_t pthread_self(void);

C函数说明

This function always succeeds, returning the calling thread's ID.

D案例说明1:

运行结果:

说明:

由于pthread_create的错误码不保存在errno中,因此不能直接用perror(3)打印错误信

息,可以先用strerror(3)把错误码转换成错误信息再打印。

E案例说明2:

运行结果:

3 pthread_exit()函数

说明:

如果任意一个线程调用了exit或_exit,则整个进程的所有线程都终止,由于从main函数return也相当于调用exit,为了防止新创建的线程还没有得到执行就终止,我们在main函数return之前延迟1秒,这只是权宜之计,即使主线程等待1秒,内核也不一定会调度新创建的线程执行。所以这时候要用到pthread_exit()

pthread_exit()函数的特点如下:

A:调用线程退出函数,注意和exit函数的区别,任何线程里exit导致进程退出,其他线程

未工作结束,主控线程退出时不能return或exit。

B:需要注意,pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

C依赖的头文件

#include<pthread.h>

D函数声明

voidpthread_exit(void*retval);

E功能:使用函数pthread_exit退出线程,这是线程的主动行为;由于一个进程中的多个线程是共享数据段的,因此通常在线程退出之后,退出线程所占用的资源并不会随着线程的终止而得到释放,但是可以用pthread_join()函数来同步并释放资源。

F说明

retval:pthread_exit()调用线程的返回值,可由其他函数如pthread_join来检索获取。

G案例说明:

运行结果:

此外:pthread_exit()函数括号中的值是用于传出的,类似exit(0)和exit(1)。这个值供pthread_join获取,用以判断是正常退出还是异常退出。

4 pthread_join()函数

A依赖的头文件

#include<pthread.h>

B函数声明

int pthread_join(pthread_t thread,void**retval);

pthread_t thread:回收线程的tid

void **retval:接收退出线程传递出的返回值

返回值:成功返回0,失败返回错误号

调用该函数的线程将挂起等待,直到id为thread的线程终止。thread线程以不同的方式终止,通过pthread_join得到的终止状态是不同的,总结如下:

  1. 如果thread线程通过return返回,value_ptr所指向的单元里存放的是thread线程函数的返回值。

  2. 如果thread线程被别的线程调用pthread_cancel异常终止掉,value_ptr所指向的单元存放的是常熟PTHREAD_CANCELED.

  3. 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数。

  4. 如果对thread线程的终止状态不感兴趣,可以传NULL给value_ptr参数。

C:函数描述:

The pthread_join() function waits for thethread specified by thread to

terminate. If that thread hasalready terminated, then pthread_join()

returns immediately. The threadspecified by thread must be joinable.

If retval is notNULL, then pthread_join() copies the exit status of

the target thread (i.e., the value that the target thread supplied to

pthread_exit(3)) into thelocation pointed to by *retval. If thetar‐

get thread was canceled, then PTHREAD_CANCELED is placed in *retval.

If multiple threads simultaneously try to join with the same thread,

the results are undefined.  If the thread callingpthread_join() is

canceled, then the target thread will remain joinable (i.e., it will

not be detached).

5 pthread_cancel

在进程内某个线程可以取消另外一个线程

A依赖的头文件

#include<pthread.h>

B函数声明

int pthread_cancel(pthread_t thread);

C函数说明:

被取消的线程,退出值,定义在Linux的pthread库中常数PTHREAD_CANCELED的值是-1.可以在头文件pthread.h中找到它的定义:

#define PTHREAD_CANCELED ((void *) -1)

D案例说明:

运行结果:

6 pthread_detach()函数

A依赖的头文件

#include<pthread.h>

B函数声明

int pthread_detach(pthread_t tid);

pthread_t tid:分离线程tid

返回值:成功返回0,失败返回错误号

C函数说明:

一般情况下,线程终止后,其终止状态一直保留到其它线程调用pthread_join获取它的

状态为止。但是线程也可以被置为detach状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留终止状态。不能对一个已经处于detach状态的线程调用pthread_join,这样的调用将返回EINVAL。对一个尚未detach的线程调用pthread_join或pthread_detach都可以把该线程置为detach状态,也就是说,不能对同一线程调用两次pthread_join,或者如果已经对一个线程调用了pthread_detach就不能再调用pthread_join了。

E案例说明

运行结果:

当把代码不部分的红框中的代码取消注释,在运行时,运行结果如下:

说明:join和detach不能同时使用

7 pthread_equal()函数

比较两个线程是否相等

A依赖的头文件

#include<pthread.h>

B函数声明

int pthread_equal(pthread_t t1,pthread_tt2);

C案例说明

运行结果:

2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(相关推荐

  1. 线程控制原语之pthread_self和pthread_create函数

    注意:使用线程库函数用gcc编译时,要加参数:-lpthread(libpthread.so),因为线程库函数属于第三方c库函数,不是标准库函数(/lib./usr/lib或者/usr/local/l ...

  2. linux系统编程:线程原语

    线程原语 线程概念 线程(thread),有时被称为轻量级进程(Lightweight Process,LWP).是程序运行流的最小单元.一个标准的线程由线程ID.当前指令指针(PC),寄存器集合和堆 ...

  3. linux 线程创建 pthread_create函数 获取线程id

    函数原型: #include<pthread.h> int  pthread_create(pthread_t*thread,pthread_attr_t   *attr, void * ...

  4. linux 线程pthread_detach,linux线程之pthread_join和pthread_detach

    在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在 被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反 ...

  5. linux中pthread_join()与pthread_detach()详解

    前言: 1.linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,如果线程是joinable状态,当线程函数自己返回退出时或pthread_e ...

  6. OS / Linux / pthread_join() 和 pthread_detach() 函数区别

    一.线程的两种状态 对于 linux 线程来说,pthread 有两种状态:joinable 和 unjoinable. 若线程的状态是 joinable,当线程函数自己返回退出时或 pthread_ ...

  7. pthread_join和pthread_detach详解

    在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反, ...

  8. 线程创建 pthread_create 中自定义参数注意事项

    2019独角兽企业重金招聘Python工程师标准>>> 1. 函数原型 int pthread_create(pthread_t *thread, const pthread_att ...

  9. linux线程随笔-pthread_create函数

    函数简介 pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int pthread_create(pthread_t  *r ...

最新文章

  1. apache环境下配置服务器支持https
  2. golang版try..catch..
  3. Android Studio问题集锦
  4. oracle取消180天过期,Oracle密码过期如何取消密码180天限制及密码180天过期,账号锁住的问题...
  5. Zookeeper 服务注册与发现02——服务消费者
  6. 科研SCI论文图片常见问题和错误汇总
  7. vscode中go插件配置
  8. 连续交付友好的Maven版本
  9. 计算机应用基础案例教程习题答案,计算机应用基础案例教程习题答案
  10. go -生成pb文件 -下
  11. 齐杰文学CMS - 关关采集器2021可用19条采集规则
  12. 织梦dedecms 模板代码标签学习
  13. java 常量pi_Java-常量
  14. 谷歌地图 经纬加密_Google开始加密搜索
  15. 零基础入门语音识别-食物声音识别
  16. 什么是back annotation
  17. 塔米狗2022年地方国企名单,总计816家企业
  18. MVC AJAXPro
  19. 函数名和变量名重名问题
  20. 狂人社区_观看此狂人将软盘驱动器连接到他的Android手机

热门文章

  1. Jupyter Notebook知识点
  2. wxWidgets:wxResourceTranslationsLoader类用法
  3. wxWidgets:wxGridUpdateLocker类用法
  4. boost::spirit模块实现在正确引用的情况下打印任何字符序列的测试程序
  5. boost::mp11::mp_map_find相关用法的测试程序
  6. boost::geometry模块Linestring多边形叠加示例
  7. Boost::context模块callcc的无限循环测试程序
  8. DCMTK:缩放DICOM图像
  9. DCMTK:DSRTree和DSRTreeNodeCursor类的测试程序
  10. VTK:可视化算法之FireFlow