Qt5.12实战之POSIX多线程库使用

该文展示了如何在Qt控制台应用程序中创建和管理线程,包括使用pthread_create创建线程,传递参数,以及处理共享数据。示例中包含了不同类型的线程函数,如无参数、带参数和使用自定义结构体参数的线程函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

演示效果 

创建一个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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自由软件开发者

有你的鼓励,我会更加努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值