您的位置:首页 > 编程语言 > C语言/C++

windows qt 使用c++ posix接口编写多线程程序(真神奇)good

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

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

1. 多线程实现

[cpp] view plain copy

#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 动作保证子线程有足够的时间调度执行。

执行效果:

[cpp] view plain copy

main() : 创建线程, 0

main() : 创建线程, 1

Hello Runoob! 线程ID, 0

main() : 创建线程, 2

main() : 创建线程, 3

Hello Runoob! 线程ID, 2

main() : 创建线程, 4

Hello Runoob! 线程ID, 1

Hello Runoob! 线程ID, 3

Hello Runoob! 线程ID, 4

2.线程的分离和链接

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

代码如下:

[cpp] view plain copy

#include <iostream>

using namespace std;

#include <cstdlib>

#include <pthread.h>

#include <unistd.h>

#include <windows.h>

#define NUM_THREADS 5

void *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,最后回收线程,执行效果如下:

[cpp] view plain copy

main() : creating thread: 0

main() : creating thread: 1

main() : creating thread: 2

main() : creating thread: 3

main() : creating thread: 4

Sleeping in thread

Thread with id: 1exiting ...!

Sleeping in thread

Thread with id: 3exiting ...!

Sleeping in thread

Thread with id: 0exiting ...!

Sleeping in thread

Thread with id: 2exiting ...!

Sleeping in thread

Thread with id: 4exiting ...!

Main:completed thread id:0

exiting with status :0

Main:completed thread id:1

exiting with status :0

Main:completed thread id:2

exiting with status :0

Main:completed thread id:3

exiting with status :0

Main:completed thread id:4

exiting with status :0

Main: 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

http://blog.csdn.net/xiaopangzi313/article/details/52791205
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: