pthread_create
创建线程
头文件:
#include <pthread.h>
原型:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
参数:
pthread_t *thread:存储创建后的线程,用tid表示该线程;
pthread_attr_t *attr:线程属性,填NULL,代表默认属性;
void *(*start_routine) (void *):是线程的执行体,函数指针。回调函数。
需要传入一个函数的地址,该指针能指向返回值是void*类型,且参数列表是void*类型的函数;
void *arg:传入回调函数的参数,如果没有填NULL;
返回值:
成功,返回0;
失败,返回非0错误码,设置errno;
例程:
void* handler1(void*arg)
{
pthread_detach(pthread_self());
pthread_exit((void*)2);
}
void* handler2(void*arg)
{
int data = *(int*)arg;
pthread_detach(pthread_self());
pthread_exit(NULL);
}
int main(void){
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, handler1, NULL) != 0)
{
perror("pthread_create");
return -1;
}
int val = 0;
if(pthread_create(&tid2, NULL, handler2, (void*)&val) != 0)
{
perror("pthread_create");
return -1;
}
int status;
pthread_join(tid1, (void**)&status);
printf("%d\n", status);
pthread_join(tid2, NULL);
}
pthread_exit
退出当前线程,可以被pthread_join函数接收到退出时的状态值
头文件:
#include <pthread.h>
原型:
void pthread_exit(void *retval);
参数:
void *retval:线程退出的状态值,可以是任意类型的数据地址,也可以填NULL;
例程:
//不传递退出状态
pthread_exit(NULL);
//传递退出状态值2
pthread_exit((void*)2);
pthread_join
阻塞等待子线程退出,并回收线程资源(相当于进程中的wait函数)
头文件:
#include <pthread.h>
原型:
int pthread_join(pthread_t thread, void **retval);
参数:
pthread_t thread:指定要等待的线程tid;
void **retval:存储的是 pthread_exit(void *retval)的参数的地址,如果不想接收,填NULL;
返回值:
成功,返回0;
失败,返回errno;
例程:
//不接收线程退出时的状态
pthread_join(tid,NULL);
//接收线程退出时的状态
int status;
pthread_join(tid, (void**)&status);
printf("%d\n", status);
例程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
//方法2:定义全局变量
//int status = 2;
//线程执行体
void* handler(void*arg)
{
printf("******线程******\n");
//方法1:定义静态局部变量
//static int status = 1;
/*
//方法1,2
pthread_exit(&status);
*/
//方法3:直接给值
pthread_exit((void*)3);
}
void* handler1(void*arg)
{
sleep(1);
printf("******线程1******\n");
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, handler, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2, NULL, handler1, NULL) != 0)
{
perror("pthread_create");
return -1;
}
printf("主函数\n");
/*
//方法1,2:定义变量 接收处理
int *sta;//指针变量存储(指向)status的地址
pthread_join(tid1, (void**)&sta);
printf("%d\n", *sta);
*/
//方法3:直接给值 接收处理
int sta;//存储给定的值
pthread_join(tid1, (void**)&sta);
printf("%d\n", sta);
return 0;
}
结果
//方法1:
主函数
******线程******
1
//方法2:
主函数
******线程******
2
//方法3:
主函数
******线程******
3
pthread_cancel
请求指定线程退出
头文件:
#include <pthread.h>
原型:
int pthread_cancel(pthread_t thread);
参数:
pthread_t thread:指定要退出的线程tid;发送成功,并不意味着线程退出;
返回值:
成功,返回0;
失败,返回error;
例程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
pthread_t tid1, tid2;
//线程执行体
void* handler1(void*arg)
{
printf("******线程1******\n");
sleep(3);
printf("%d\n",pthread_cancel(tid2));
printf("******线程1111******\n");
pthread_exit((void*)2);
}
void* handler2(void*arg)
{
sleep(1);
printf("******线程2******\n");
while(1)
sleep(1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(pthread_create(&tid1, NULL, handler1, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2, NULL, handler2, NULL) != 0)
{
perror("pthread_create");
return -1;
}
printf("主函数\n");
pthread_join(tid2, NULL);
printf("线程2退出\n");
int status;
pthread_join(tid1, (void**)&status);
printf("%d\n", status);
printf("线程1退出\n");
return 0;
}
结果
主函数
******线程1******
******线程2******
0
******线程1111******
线程2退出
2
线程1退出
pthread_detach
分离主线程与子线程,子线程退出后,自动回收子线程资源,不会传递状态给pthread_cancel,pthread_cancel也不会阻塞等待
头文件:
#include <pthread.h>
原型:
int pthread_detach(pthread_t thread);
参数:
pthread_t thread:指定要分离的线程tid;
返回值:
成功,返回0;
失败,返回errno;
例程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
//线程执行体
void* handler1(void*arg)
{
pthread_detach(pthread_self());
int i = 3;
while(i--)
{
printf("******线程1******\n");
sleep(1);
}
printf("******线程1111******\n");
pthread_exit((void*)2);
}
void* handler2(void*arg)
{
sleep(1);
while(1)
{
printf("******线程2******\n");
sleep(1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, handler1, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2, NULL, handler2, NULL) != 0)
{
perror("pthread_create");
return -1;
}
int i = 2;
while(i--)
{
printf("主函数.....\n");
sleep(1);
}
int status;
pthread_join(tid1, (void**)&status);
printf("%d\n", status);
printf("线程1退出\n");
pthread_join(tid2, NULL);
printf("线程2退出\n");
return 0;
}
没有注释第7行时,执行 pthread_detach 结果
主函数.....
******线程1******
主函数.....
******线程2******
******线程1******
134514747
线程1退出
******线程2******
******线程1******
******线程2******
******线程1111******
******线程2******
******线程2******
******线程2******
******线程2******
^C
注释第7行时,不执行 pthread_detach 的结果
主函数.....
******线程1******
主函数.....
******线程2******
******线程1******
******线程2******
******线程1******
******线程2******
******线程1111******
2
线程1退出
******线程2******
******线程2******
******线程2******
******线程2******
^C
pthread_self
获取当前线程的tid号
头文件:
#include <pthread.h>
原型:
pthread_t pthread_self(void);
返回值:
返回当前线程的tid值;
本文链接:https://shengto.top/c/thread_routine.html
转载时须注明出处及本声明