头文件
#include <pthread.h>
线程的操作
线程操作包括3 种线程的创建,退出,等待
线程的创建
函数原型
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
参数
tidp
:当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID。
attr:
一般设为NULL。attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。
(*start_rtn)(void *)
:新创建的线程的入口地址。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。
arg
:向start_rtn函数传递的参数。如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构体中,然后把这个结构体的地址作为arg参数传入。
返回值
成功返回0,否则返回错误编号
线程的退出
单个线程可以通过以下三种方式退出,在不终止整个进程的情况下停止它的控制流:
(1)线程只是从启动例程中返回,返回值是线程的退出码。
(2)线程可以被同一进程中的其他线程取消。
(3)线程调用pthread_exit:
函数原型
int pthread_exit(void *rval_ptr);
函数参数
rval_ptr:是一个无类型指针,与传给启动例程的单个参数类似。进程中的其他线程可以通过调用pthread_join函数访问到这个指针。
线程的等待
调用这个函数的线程将一直阻塞,直到指定的线程调用pthread_exit、从启动例程中返回或者被取消。
函数原型
int pthread_join(pthread_t thread, void **rval_ptr);
函数参数
thread
:被等待的线程ID
**rval_ptr
:用户定义的指针,用来存储被等待线程的返回值。如果对线程的返回值不感兴趣,可以把rval_ptr置为NULL。在这种情况下,调用pthread_join函数将等待指定的线程终止,但并不获得线程的终止状态。
返回值
成功返回0,否则返回错误编号
其他API
线程脱离
函数原型
int pthread_detach(pthread_t thread);
函数参数
线程标识符
当我们的线程运行结束后,最后显示的调用被回收。这样就出现两种回收方式。
- pthread_join是一个阻塞函数,调用方会阻塞到pthread_join所指定的tid的线程结束后才被回收,但是在此之前,调用方是霸占系统资源的。
- pthread_detach,不会阻塞,调用它后,线程运行结束后会自动释放资源。
返回值
返回:若成功返回0,否则返回错误编号
本函数通常由想让自己脱离的线程使用,就如以下语句:
pthread_detach(pthread_self());
线程ID获取
函数原型
pthread_t pthread_self(void);
返回值
返回:调用线程的ID
线程ID比较
对于线程ID比较,为了可移植操作,我们不能简单地把线程ID当作整数来处理,因为不同系统对线程ID的定义可能不一样。我们应该要用下边的函数:
函数原型
int pthread_equal(pthread_t tid1, pthread_t tid2);
函数参数
tid1:线程1的ID
tid2:线程2的ID
返回值
返回:若相等则返回非0值,否则返回0
三大操作的示例
示例1(线程退出,返回数字)
#include<stdio.h>
#include<pthread.h>
void *func1(void*arg)
{
static int param=10;//不用静态,函数执行结束后数据会没掉
printf("t1:%ld thread create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int*)arg));
pthread_exit((void *)¶m);
return 0;
}
int main()
{
int ret;
int *pret;
int param=100;
pthread_t t1;
ret=pthread_create(&t1,NULL,func1,(void*)¶m);
if(ret==0){
printf("main: create t1 success!\n");
}
printf("main:%ld \n",(unsigned long)pthread_self());
pthread_join(t1,(void**)&pret);//注意理解二级指针的用法
printf("%d \n",*pret);
return 0;
}
示例2(线程退出,返回字符串)
#include<stdio.h>
#include<pthread.h>
void *func1(void*arg)
{
static char *param="abc";
printf("t1:%ld thread create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int*)arg));
pthread_exit((void *)param);
return 0;
}
int main()
{
int ret;
char *pret;
int param=100;
pthread_t t1;
ret=pthread_create(&t1,NULL,func1,(void*)¶m);
if(ret==0){
printf("main: create t1 success!\n");
}
printf("main:%ld \n",(unsigned long)pthread_self());
pthread_join(t1,(void**)&pret);
printf("%s\n",pret);
return 0;
}