Win10+VS2017配置pthread


0、pthread源碼下載:https://sourceware.org/pthreads-win32/

1、下載pthreads-w32-2-9-1-release.zip完畢后,解壓,內容如下

 

 其中,【Pre-built.2】是pthreads for win32的頭文件和庫文件,【pthreads.2】是源代碼,【QueueUserAPCEx】是一個驅動,需要WDK支持編譯。

2、將【.\pthreads-w32-2-9-1-release\Pre-built.2\include】目錄下的頭文件拷貝到VS2017的安裝目錄,當前環境下是【C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\include

3、把【.\pthreads-w32-2-9-1-release\Pre-built.2\lib】下的靜態庫文件拷貝到VS2017的安裝目錄,當前環境下是【C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\lib】,x86和x64對 

4、把【.\pthreads-w32-2-9-1-release\Pre-built.2\dll】下的動態庫文件拷貝到系統目錄下,x86文件對應C:\Windows\SysWOW64目錄,x64文件對應C:\Windows\System32目錄

 

也可以在某個項目中引用該庫,或者用更加時髦的Nuget也可以找到這個庫。

配置好后,可用以下代碼進行測試:

 1 #include "pch.h"
 2 #include <iostream>
 3 #include <stdio.h>
 4 #include <pthread.h>
 5 #include <assert.h>
 6 
 7 #pragma comment(lib,"x86/pthreadVC2.lib")
 8 
 9 void* Function_t(void* Param)
10 {
11     std::cout << "多線程 " << std::endl;
12     pthread_t myid = pthread_self();
13     std::cout << "線程ID=" << myid.x << std::endl;
14     return NULL;
15 }
16 
17 int main(int argc, const char *argv[])
18 {
19     pthread_t pid;
20     pthread_attr_t attr;
21     pthread_attr_init(&attr);
22     pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
23     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
24     pthread_create(&pid, &attr, Function_t, NULL);
25     std::cout << "======================================== " << std::endl;
26     getchar();
27     pthread_attr_destroy(&attr);
28     return 0;
29 }

如果在編譯時報錯【C2011 “timespec”:“struct”類型重定義】,原因是【pthread.h 中的 timespec 和 time.h 中的結構定義重復了,同時兩個頭文件中的編譯條件不同,造成了結構的重復定義】,

解決方案:

#if !defined( PTHREAD_H )
#define PTHREAD_H
下面加上
#define HAVE_STRUCT_TIMESPEC

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM