演示效果
创建一个Qt控制台应用,然后复制最后面的示例源码,然后运行可显示 上图内容
1.使用前要先引用两个头文件
#include <unistd.h>
#include <pthread.h>
2.创建线程函数
//自定义线程函数,支持返回空指针,支持NULL参数的线程函数
void *thread_func(void *arg)
{
printf("thread_func:%s\n","这是在线程中执行输入的内容");
return (void*)0;
}
3.创建线程
pthread_t tid;//线程ID
//在进程中创建线程
int result = pthread_create(&tid,//线程ID指针
NULL,//线程属性
thread_func,//线程函数
NULL//线程函数参数
);
4.链接或者分离线程
pthread_join(tid,NULL);
pthread_detach(tid);
完整示例代码:
#include <QCoreApplication>
#include <unistd.h>
#include <pthread.h>
int count = 1;//进程主线程全局变量count
//自定义结构体
typedef struct MyStruct
{
//结构构造函数,构造时不传入参数,取默认值
MyStruct(const int _age=NULL,const char* _name=NULL) {
age= _age ? _age : 888;
name= _name ? _name : "入侵破解比打工强多了";
}
int age;
const char *name;
};
typedef struct
{
int id;
char *work;
}MyJob;
//自定义线程函数,支持返回空指针,支持NULL参数的线程函数
void *thread_func(void *arg)
{
printf("thread_func:%s\n","这是在线程中执行输入的内容");
return (void*)0;
}
//自定义线程函数,支持返回空指针,支持NULL参数的线程函数
void *thread_func_with_param(void *arg)
{
char *str = (char*)arg;//指针强制转换
printf("thread_func_with_param:调用线程输入的参数->%s\n",str);
return (void*)0;
}
//自定义线程函数,支持返回空指针,支持NULL参数的线程函数
void *thread_func_with_cust_struct(void *arg)
{
MyStruct *pMyStruct = (MyStruct*)arg;//强制传入参数类型转换
printf("thread_func_with_cust_struct:调用线程传入的结构MyStruct->{age:%d,name:%s}\n",
pMyStruct->age,pMyStruct->name);
return (void *)0;
}
//自定义线程函数,支持返回空指针,支持NULL参数的线程函数
void *pthread_func_use_shared_data(void *arg)
{
count *=20;
printf("pthread_func_use_shared_data:%s:%d\n","共享数据count被线程pthread_func_use_shared_data修改后的值",count);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
pthread_t tid;//线程ID
//在进程中创建线程
int result = pthread_create(&tid,//线程ID指针
NULL,//线程属性
thread_func,//线程函数
NULL//线程函数参数
);
//线程创建成功返回0,错误返回非0
if(result)
{
printf("main:%s\n","指向thread_func函数的线程创建失败");
}else{
printf("main:%s\n","线程创建成功");
sleep(1);//主线程休眠1秒
printf("main:%s\n","主线输出的内容");
}
//创建线程并传入参数
result = pthread_create(&tid,//线程ID
NULL,//线程参数
thread_func_with_param,//线程函数
(void *)"主线程传入的参数");//参数类型要强制转换为void*
if(result){
printf("main:%s\n","指向thread_func_with_param函数的线程创建失败");
}else{
printf("main:%s\n","指向执行函数thread_func_with_param的线程创建成功!");
//阻塞主线程,直到子线程执行完成并返回
printf("main:%s\n","主线main阻塞中,子线程thread_func_with_param执行中..");
pthread_join(tid,NULL);
printf("main:%s\n","子线程执行完成,返回主线程main");
}
//构造时使用默认值,不传入参数时不用加()
MyStruct mystruct;
//构造时直接赋值
//MyStruct mystruct(999,"remotedev");
//重新赋值结构成员
//mystruct.age = 999 ;
//mystruct.name = "remotedev";
//创建线程并传入自定义结构体
result = pthread_create(&tid,
NULL,
thread_func_with_cust_struct,
(void*)&mystruct);//转换指针要使用结构地址,不能直接使用结构
if(result){
printf("main:%s\n","指向thread_func_with_cust_struct函数的线程创建失败");
}else{
printf("%s\n","指向thread_func_with_cust_struct函数的线程创建成功!");
//阻塞主线程,直到子线程执行完成并返回
printf("main:%s\n","主线main阻塞中,子线程thread_func_with_cust_struct执行中..");
pthread_join(tid,NULL);
printf("main:%s\n","子线程执行完成,返回主线程main");
}
//线程共享进程的数据
printf("main:%s:%d\n","共享数据count默认值",count);
result = pthread_create(&tid,//线程ID
NULL,//线程属性
pthread_func_use_shared_data,//线程函数
NULL);//线程函数参数
if(result){
printf("main:%s\n","指向pthread_func_use_shared_data函数的线程创建失败");
}else{
printf("main:%s\n","指向pthread_func_use_shared_data函数的线程创建成功!");
printf("main:%s\n","主线main阻塞中,子线程pthread_func_use_shared_data执行中..");
pthread_join(tid,NULL);
printf("main:%s\n","子线程执行完成,返回主线程main");
count-=5;
printf("main:%s:%d\n","主线程main修改后共享数据count的值",count);
}
return a.exec();
}