线程创建及相关函数
1 pthread_create:创建线程
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
成功:返回0,否则返回错误编号
- 参数
- thread:线程标识符指针
- attr:线程属性指针
- start_routine:线程运行函数的起始地址
- arg:传递给线程运行函数的参数
- 新创建线程从start_rtn函数的地址开始运行
- 不能保证新线程和调用线程的执行顺序
2 pthread_t pid:线程号
3 pthread_attr_t :线程属性(分离式线程,自动回收线程资源)
typedef struct
{
int detachstate; // 线程的分离状态
int schedpolicy; // 线程调度策略
structsched_param schedparam; // 线程的调度参数
int inheritsched; // 线程的继承性
int scope; // 线程的作用域
size_t guardsize; // 线程栈末尾的警戒缓冲区大小
int stackaddr_set; // 线程的栈设置
void* stackaddr; // 线程栈的位置
size_t stacksize; // 线程栈的大小
} pthread_attr_t;
4 pthread_attr_init:初始化线程属性
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr);
功能:使用默认属性值初始化attr指向的线程属性对象
返回:成功返回0,出错返回一个非零错误数
int pthread_attr_destroy(pthread_attr_t *attr);
功能:销毁线程属性对象
返回:成功返回0,出错返回一个非零错误数
5 pthread_attr_setetachstate():设置属性
#include <pthread.h>
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
功能:设置所引用的线程属性对象的分离状态属性attr设置为detachstate中指定的值
可设置值选项:
可以在detachstate中指定以下值:
PTHREAD_CREATE_DETACHED:使用attr创建的线程将以分离状态创建 。
PTHREAD_CREATE_JOINABLE:使用attr创建的线程将以可连接状态创建。
新初始化的线程属性对象中分离状态属性的默认设置为PTHREAD_CREATE_JOINABLE
int pthread_attr_getdetachstate(const pthread_attr_t *attr,int *detachstate);
功能:返回缓冲区中线程属性对象attr的分离状态属性由detachstate指向。
6 线程终止方式
#include <pthread.h>
int pthread_cancel(pthread_t thread);
功能:向线程发送一个取消请求
返回:成功返回0,出错返回一个非零错误数
#include <pthread.h>
void pthread_exit(void *retval);
功能:终止调用线程
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
功能:等待线程指定的线程终止
返回:成功返回0,出错返回一个错误数
- pthread_cancel
- 线程可以被同一进程的其他线程取消,thread为被终止的线程标识符。
- pthread_exit:
- Retval:pthread_exit调用者线程的返回值,可由其他函数和pthread_join来检测获取。
- 线程退出时使用函数pthread_exit,是线程的主动行为。
- 由于一个进程中的多个线程共享数据段,因此通常在线程退出后,退出线程所占用的资源并不会随线程结束而释放。所有需要pthread_join函数来等待线程结束。,类似于wait系统调用。
- pthread_join
- thread:被等待线程的标识符
- retval:用户定义指针,用来存储被等待线程的返回值
- 主动终止
- 线程的执行函数中调用return语句
- 调用pthread_exit()
- 被动终止
- 线程可以被同一进程的其他线程取消,其它线程调用pthread_cancel(pthid).
7 其它线程函数
#include <pthread.h>
void pthread_cleanup_push(void (*routine)(void *),void *arg);
功能:将例程推送到清理处理程序堆栈的顶部
void pthread_cleanup_pop(int execute);
功能:删除清理处理程序堆栈顶部的例程
参数:
- routine:清理函数指针
- arg:调用清理函数传递的参数
- execute:值1时执行线程清理函数,值0时不执行线程清理函数。
触发线程调用不理函数的动作 - 调用pthread_exit
- 响应取消请求
- 用非零execute参数调用thread_cleanup_pop时
8 示例代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void* pthread_demo(void* arg)
{
int* flag = (int*)arg;//arg是pthread_create函数创建线程时,第四个参数
while(*flag){
printf("我是线程\n");
sleep(1);
}
printf("子线程结束\n");
return NULL;
}
int main(void)
{
pthread_t pid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//设置属性为分离式线程,线程结束时自动回收线程资源
//创建线程
int flag = 1;
pthread_create(&pid, &attr, pthread_demo, &flag);//创建新的线程,作pthread_demo
printf("创建线程成功\n");
while(flag){
//scanf("%d", &flag);
printf("我是主线程\n");
sleep(1);
}
//回收线程资源
//pthread_join(pid, NULL);
return 0;
}