windows qt 使用c++ posix接口编写多线程程序(一)

来源:互联网 发布:知乎成功的原因 编辑:程序博客网 时间:2024/04/30 11:43

一、多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序。一般情况下,两种类型的多任务处理:基于进程和基于线程基于进程的多任务处理是程序的并发执行。基于线程的多任务处理是同一程序的片段的并发执行。多线程程序包含可以同时运行的两个或多个部分。这样的程序中的每个部分称为一个线程,每个线程定义了一个单独的执行路径,C++ 不包含多线程应用程序的任何内置支持。相反,它完全依赖于操作系统来提供此功能

二、多线程的案例(以下案例都在windows qt 环境下编译运行)

1. 多线程实现

#include <iostream>//#include <pthread.h>#include <time.h>#include "pthread.h"using namespace std;#define NUM_THREADS 5//c++ 中实现延时函数void delay(int sec){    time_t start_time, cur_time; // 变量声明    time(&start_time);    do {    time(&cur_time);    } while((cur_time - start_time) < sec );}void *say_hello(void *threadid){    //对传入的参数进行强制类型转换    int tid = *((unsigned short *)threadid);    cout << "Hello Runoob! 线程ID, "<< tid << endl;    pthread_exit(NULL);}int main(){    //定义线程id变量    pthread_t tids[NUM_THREADS];    int indexes[NUM_THREADS];    for(int i = 0;i < NUM_THREADS; ++i){        cout << "main() : 创建线程, " << i << endl;        indexes[i] = i;//先保存i的值        int ret = pthread_create(&tids[i],NULL,say_hello,(void *)&indexes[i]);        if(ret != 0){            cout << "pthread_create error : error_code="<< ret << endl;        }    }    delay(2);    pthread_exit(NULL);    return 0;}
上述案例使用pthread_create创建线程,参数可以传入线程入口地址,调用成功后直接进入线程入口函数,入口函数代码即为线程体,在线程体执行完毕后调用pthread_exit结束线程,main函数就是一主线程,在其创建的线程都是其子线程,子线程依附于主线程,若主线程提前结束,子线程也会退出,为了保证子线程能够正常退出,在main线程中执行了delay 动作保证子线程有足够的时间调度执行。

执行效果:

main() : 创建线程, 0main() : 创建线程, 1Hello Runoob! 线程ID, 0main() : 创建线程, 2main() : 创建线程, 3Hello Runoob! 线程ID, 2main() : 创建线程, 4Hello Runoob! 线程ID, 1Hello Runoob! 线程ID, 3Hello Runoob! 线程ID, 4

2.线程的分离和链接

在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached)。一个可结合的线程能够被其他线程收回其资源和杀死;在被其他线程回收之前,它的存储器资源(如栈)是不释放的。相反,一个分离的线程是不能被其他线程回收或杀死的,它的存储器资源在它终止时由系统自动释放。线程的分离状态决定一个线程以什么样的方式来终止自己。在默认情况下线程是非分离状态的,这种情况下,原有的线程等待创建的线程结束。只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源。而分离线程不是这样子的,它没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。程序员应该根据自己的需要,选择适当的分离状态。所以如果我们在创建线程时就知道不需要了解线程的终止状态,则可以pthread_attr_t结构中的detachstate线程属性,让线程以分离状态启动.

代码如下:

#include <iostream>using namespace std;#include <cstdlib>#include <pthread.h>#include <unistd.h>#include <windows.h>#define NUM_THREADS 5void *wait(void *t){   int i;   long tid;   tid = (long)t;   Sleep(1000);   cout <<"Sleeping in thread"<< endl;   cout <<"Thread with id: " << tid << "exiting ...!" << endl;   pthread_exit(NULL);}int main(){    int rc,i;    pthread_t theads[NUM_THREADS];    pthread_attr_t attr;    void *status;    //初始化并设置线程为可连接的(joinable)    pthread_attr_init(&attr);    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);    for(i = 0; i < NUM_THREADS ; i ++){        cout << "main() : creating thread: " << i <<endl;        rc = pthread_create(&theads[i],NULL,wait,(void *)i);        if(rc){            cout << "Error:uable to create thread," << endl;            exit(-1);        }    }    //删除属性并等待其他线程    pthread_attr_destroy(&attr);    for(i = 0; i < NUM_THREADS; i ++){        rc = pthread_join(theads[i],&status);        if(rc){            cout <<"Uable to join," << endl;            exit(-1);        }        cout << "Main:completed thread id:" << i << endl;        cout << "exiting with status :" << status << endl;    }    cout << "Main: program exiting." << endl;    pthread_exit(NULL);    return 0;}
案例中先创建线程,然后设置线程的属性为joinable,最后回收线程,执行效果如下:

main() : creating thread: 0main() : creating thread: 1main() : creating thread: 2main() : creating thread: 3main() : creating thread: 4Sleeping in threadThread with id: 1exiting ...!Sleeping in threadThread with id: 3exiting ...!Sleeping in threadThread with id: 0exiting ...!Sleeping in threadThread with id: 2exiting ...!Sleeping in threadThread with id: 4exiting ...!Main:completed thread id:0exiting with status :0Main:completed thread id:1exiting with status :0Main:completed thread id:2exiting with status :0Main:completed thread id:3exiting with status :0Main:completed thread id:4exiting with status :0Main: program exiting.
三、移植环境搭建

大家可以注意到,上述的程序都是posix pthread接口即在Linux下使用的api,在win下默认是不能编译通过,所以编译之前我们需要做好移植工作,如下:

1.下载windows支持的posix pthread库,路径:点击打开链接

2.解压库代码:

解压pthreads-w32-2-7-0-release .rar到D盘,库路径为D:\Documents\pthreadlib\Pre-built.2\

3.在QT中指定库的路径:

LIBS += -LD:\Documents\pthreadlib\Pre-built.2\lib -lpthread




0 0
原创粉丝点击