1.引用头文件
#include <pthread.h>
2.定义线程执行函数及实现
void *thread_func(void* arg);//声明
//实现
//线程执行函数实现
void *thread_func(void* arg)
{
int r = *(int*)&arg;//先转换为整型指针,然后解引用
cout<<r<<endl;
for (int i = 0; i <r; ++i) {
cout<<"子线程运算:"<<i+1<<endl;
}
}
3.创建线程并启动
int threadArg=35;
pthread_t tid;//线程对象
pthread_create(&tid,//线程对象指针
NULL,//线程属性
thread_func,//线程执行函数
(void*)threadArg);//线程执行函数参数
pthread_detach(tid);//启动线程并