IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Visual Studio中配置pthreads

    TacuLee发表于 2015-09-17 03:16:30
    love 0

    从http://sourceware.org/pthreads-win32/下载pthread 的windows安装包,我下的是pthread-w32-2-8-0-release.exe,其他版本也可以。解压到pthread-w32-2-8-0-release。

    本文使用vs2015,其于版本大同小异。打开vs2015,项目->属性->配置属性->VC++目录,包含目录里添加inlude路径,如下图所示,如果刚下载的压缩包放在D盘,则在包含目录那一栏添加:D:\pthread-w32-2-8-0-release\Pre-built.2\include;在库目录那一栏添加:D:\pthrea-w32-2-8-0-release\Pre-built2\lib

    在链接器—>输入,附加依赖项一栏添加

    pthreadVC2.lib;pthreadVCE2.lib;pthreadVSE2.lib;如下图所示。所有设置完成后点确定。

    打开pthread-w32-2-8-0-release\Pre-built.2\lib,将里面三个*.lib文件复制到你所建立的工程目录中去,这样就设置好了,大功告成。

    运行如下程序:

    #include<pthread.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<Windows.h>
    
    #define NUMBER_OF_THREADS 10
    #define null NULL
    
    void *print_hello_world(void *tid){
    	printf("\n Hello World. %d0,tid");
    	pthread_exit(null);
    
    	return 0;
    }
    
    int main(int argc, char *argv[]){
    	pthread_t threads[NUMBER_OF_THREADS];
    	int status, i;
    
    	for (i = 0; i < NUMBER_OF_THREADS; i++){
    		printf("\n Main here. Creating thread %d 0,i");
    		status = pthread_create(&threads[i],NULL,print_hello_world, (void *)i);
    
    		if (status != 0){
    			printf("\n pthread_create returned error code %d 0, status");
    			exit(-1);
    		}
    	}
    	exit(null);
    }

    这里可能会一个重定义的错,把那几行重定义的注释掉即可。可以得到运行结果为:

    未经允许不得转载:TacuLee » Visual Studio中配置pthreads



沪ICP备19023445号-2号
友情链接