【C/C++多线程编程之一】VC6.0安装pthread

来源:互联网 发布:js判断按钮是否被点击 编辑:程序博客网 时间:2024/06/13 10:29
多线程是C/C++的一个重要的概念,在windows下,需要安装pthread来支持多线程编程。以下配置博主亲测成功。
1.下载pthreads-w32-2-8-0-release.zip
          为了方便读者,同时避免不同版本带来不必要的麻烦,在此贴上本次配置使用的pthread,免积分哦:
           pthread下载
2.解压pthreads-w32-2-8-0-release.zip
           解压后如下:
         
3.运行pthreads-w32-2-8-0-release.exe
          弹出的对话框点击:Extract,结束后点击Done完成。运行后如下:
           
          我们需要的文件夹是Pre-built.2,另外两个文件夹不需要。
3.拷贝include内文件
          点开Pre-built.2文件夹:
         
          将include内的3个文件(如下):
         
          将这3个文件复制到VC6.0对应的include文件夹内:我的路径是:E:\Mysoftware\VC98\Include
4.拷贝bin内文件
          点开include下面的bin文件夹(含10个文件如下):
          
           将这10个文件复制到VC6.0对应的bin文件夹内:我的路径是:E:\Mysoftware\VC98\Bin
5.将pthreadVC2.dll添加到系统
          如果用Win8,将上面bin文件中的pthreadVC2.dll复制到:C:\Windows\System 
           
          其他Windows操作系统,则复制到:C:\Windows\System32(注Win8最好也复制一个到这个文件夹,可以提供32位程序支持)。
6.测试程序
            
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#pragma comment(lib, "pthreadVC2.lib") //必须加上这句
void* tprocess1(void* args){
int i=1;
while(i<=100){
printf("process1:%d\n",i);
i++;
}
return NULL;
}
void* tprocess2(void* args){
int i=1;
while(i<=100){
printf("process2:%d\n",i);
i++;
}
return NULL;
}
int main(){
pthread_t t1;
pthread_t t2;
pthread_create(&t1,NULL,tprocess1,NULL);
pthread_create(&t2,NULL,tprocess2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
 来自CODE的代码片
pthreadtest.c
          读者只需运行一下这个测试程序即可,以后会学习关于Pthread的更多知识。
          
          这便是线程1和线程2交替运行的结果啦!NICE!
          开始多线程编程之旅吧!
0 0
原创粉丝点击