2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(

简介:  1 pthread_create()函数 创建线程 A:依赖的头文件 #include<pthread.h> B:函数声明 int pthread_create(pthread_t *thread, constpthread_attr_t *attr, void *(*start_routine) (void *), void *


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_createarg

数传递给它的,该参数的类型为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参数表示线程属性,本节不深入讨论线程属性,所有代码例子都传NULLattr

数,表示线程属性取缺省值,感兴趣的读者可以参考[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导致进程退出,其他线程

未工作结束,主控线程退出时不能returnexit

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,失败返回错误号

 

调用该函数的线程将挂起等待,直到idthread的线程终止。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线程的终止状态不感兴趣,可以传NULLvalue_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函数说明:

被取消的线程,退出值,定义在Linuxpthread库中常数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_joinpthread_detach都可以把该线程置为detach状态,也就是说,不能对同一线程调用两次pthread_join,或者如果已经对一个线程调用了pthread_detach就不能再调用pthread_join了。

E案例说明

运行结果:

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

说明:joindetach不能同时使用

 

7 pthread_equal()函数

比较两个线程是否相等

A依赖的头文件

#include<pthread.h>

B函数声明

int pthread_equal(pthread_t t1,pthread_tt2);

C案例说明

运行结果:

目录
相关文章
|
9月前
|
Linux API
Linux系统应用编程 --- 线程原语
Linux系统应用编程 --- 线程原语
58 0
|
9月前
|
NoSQL Unix Linux
Linux系统应用编程---线程原语
Linux系统应用编程---线程原语
66 0
|
Linux C语言
【Linux线程】二、线程控制原语
【Linux线程】二、线程控制原语
132 0
【Linux线程】二、线程控制原语
|
18天前
|
存储 Java 数据库连接
java多线程之线程通信
java多线程之线程通信
|
30天前
|
存储 缓存 NoSQL
Redis单线程已经很快了6.0引入多线程
Redis单线程已经很快了6.0引入多线程
31 3
|
1月前
|
消息中间件 安全 Linux
线程同步与IPC:单进程多线程环境下的选择与权衡
线程同步与IPC:单进程多线程环境下的选择与权衡
58 0
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
1月前
|
存储 安全 Java
深入理解 Java 多线程、Lambda 表达式及线程安全最佳实践
线程使程序能够通过同时执行多个任务而更有效地运行。 线程可用于在不中断主程序的情况下在后台执行复杂的任务。 创建线程 有两种创建线程的方式。 扩展Thread类 可以通过扩展Thread类并覆盖其run()方法来创建线程:
111 1
深入理解 Java 多线程、Lambda 表达式及线程安全最佳实践