作者:小 琛
欢迎转载,请标明出处
首先明确以下几点:
- 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以“pthread_”打头的,因此所有的函数均为库函数
- 要使用这些函数库,要通过引入头文<pthread.h>
- 链接这些线程函数库时要使用编译器命令的“-lpthread”选项
创建线程
int pthread_create(pthread_t* thread,const pthread_t attr_t* attr,
void* (* thread_start)(void*),void* arg)
参数内容:
thread:线程标识符(并不是线程id),是该线程独有空间的首地址,通过这个标识符可以堆当前的线程进行操作,调用pthread_creat作为参数返回给调用者
attr:线程属性,pthread_attr_t是一个结构体,这个结构体完成对新创建线程属性的设置,使用中通常设置位NULL,即默认属性
thread_start:线程入口函数,接收一个函数的地址,该函数返回值为void*,参数为void*
arg:线程入口参数的参数,void*类型兼容自定义类型,不可以传递临时变量,允许传递堆上的值
注意:如果传入的值为堆上开辟的,在线程结束的时候要进行释放,否则会造成内存泄漏问题
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
typedef struct thread_info
{
int thread_num_;
thread_info()
{
thread_num_ = -1;
}
}THREADINFO;
void* thread_start(void* arg)
{
THREADINFO* ti = (THREADINFO*)arg;
while(1)
{
printf("i am new thread~~~~: %d\n", ti->thread_num_);
sleep(1);
}
//对于传递进来的对上开辟的内存,可以在线程入口函数结束的时候,释放掉,不会导致程序有内存泄漏的风险
delete ti;
return NULL;
}
int main()
{
pthread_t tid;
int ret;
int i = 0;
for(; i < 4; i++)
{
THREADINFO* threadinfo = new thread_info();
//堆上开辟的
threadinfo->thread_num_ = i;
ret = pthread_create(&tid, NULL, thread_start, (void*)threadinfo);
if(ret < 0)
{
perror("pthread_create");
return 0;
}
}
while(1)
{
printf("i am main thread~~~\n");
sleep(1);
}
return 0;
}
线程终止
- 从线程的入口函数的return返回
- pthread_exit(void* retval),该函数谁调用谁退出,retval参数为当前线程的退出信息,可以传入NULL
- pthread_cancel(pthread_t thread),调用该函数的执行流,可以传入自己的线程标识符用来终止自己,获取自己的线程表示符:pthread_self()
线程等待与线程分离
我们采用默认属性创建的线程会有一个属性:joinable属性,即当线程退出的时候,依赖其它的执行流来回收它的资源,否则在该线程退出的时候,由于资源未被回收,导致内存泄漏
线程等待
pthread_join(pthread_t thread,void**value_ptr)
作用:
使用该函数的线程将被挂起,直到id为thread的线程退出
函数参数:
thread:线程ID
value_ptr:它指向一个指针,后者指向线程的返回值
线程分离
设置线程的joinable属性为detach属性,程序不用去关心线程退出的时候由于线程的joinable属性带来的内存泄漏问题,detach属性不需要其它线程回收资源,在线程退出的时候由操作系统回收
pthread_detach(pthread_t thread)
thread:该线程标识符表示将哪一个线程设置为detach属性
void *thread_run( void * arg )
{
pthread_detach(pthread_self());
printf("%s\n", (char*)arg);
return NULL;
}