天天看點

VS2015中配置Pthread

一、安裝平台

       Win7\8\10   64位           Microsoft Visual Studio 2015

二、下載下傳

pthreads-w32-2-9-1

tp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip

解壓後共有三個檔案夾

Pre-built.2

pthreads.2

QueueUserAPCEx

打開Pre-built.2

dll                 ——>動态連結庫

include         ——>頭檔案

lib                 ——>靜态連結庫

三、配置頭檔案及靜态連結庫

把include檔案夾中的三個檔案直接拷貝到Visual Studio安裝目錄下VC->include檔案夾下,例如我将include中檔案拷貝到的位置是

E:\Software\Microsoft Visual Studio 11.0\VC\include

把lib檔案夾下的内容拷貝到Visual Studio安裝目錄下預設的lib尋找路徑中,即VC->lib中,例如我将lib檔案夾下的x64與x86兩個檔案直接拷貝到

E:\Software\Microsoft Visual Studio 11.0\VC\lib

四、配置動态連結庫

把dll下的x64檔案夾下的兩個檔案,即pthreadGC2.dll與pthreadVC2.dll拷貝到C:\Windows\System32下(用于64位程式的運作)

把dll下的x86檔案夾下的五個檔案,拷貝到C:\Windows\SysWOW64下(用于32位程式的運作)

五、運作測試

#include <pthread.h>

#include <stdio.h>

#define NUM_THREADS 5

#pragma comment(lib,"pthreadVC2.lib")    //必不可少,這是告訴編譯器在編譯形成的.obj檔案和.exe檔案中加一條資訊,使得連結器在連結庫的時候要去找pthreadVC2.lib這個庫,不要先去找别的庫。(.exe檔案找DLL 也是這種寫法,例如 pthreadVC2.dll)

void *PrintHello(void *threadid)

{

int tid;

tid = (int)threadid;

printf("Hello World!It's me,thread #%d!\n", tid);

pthread_exit(NULL);

}

int main(int argc,char *argv[])

{

pthread_t threads[NUM_THREADS];

int rc, t;

for (t = 0; t < NUM_THREADS; t++)

{

printf("In main:creating thread %d\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);

if (rc)

{

printf("ERROR:return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

pthread_exit(NULL);

}

繼續閱讀