0, pthread source download: https://sourceware.org/pthreads-win32/
1, after downloading pthreads-w32-2-9-1-release.zip complete, extract, reads as follows
Where [is] Pre-built.2 pthreads for win32 header files and libraries, source code [pthreads.2], [] is a QueueUserAPCEx drive, need support WDK compiler.
[. \ \ Pre-built.2 \ include pthreads-w32-2-9-1-release] 2, the header file directory is copied to the install directory VS2017, under the current environment is [C: \ Program Files (x86 ) \ Microsoft Visual Studio \ 2017 \ Enterprise \ VC \ Tools \ MSVC \ 14.16.27023 \ include
[. \ Pthreads w32-2-9-1-release-\ Pre-built.2 \ lib] 3, the static library files are copied to the installation directory VS2017, the current environment is [C: \ Program Files (x86 ) \ Microsoft Visual Studio \ 2017 \ Enterprise \ VC \ Tools \ MSVC \ 14.16.27023 \ lib], x86, and x64 for
[. \ Pthreads-w32-2-9-1-release \ Pre-built.2 \ dll] 4, the dynamic library files are copied to the system directory, x86 file corresponding to the C: \ Windows \ SysWOW64 directory, x64 file corresponding to the C: \ Windows \ System32 directory
The library can also be referenced in a project or in a more fashionable Nuget can also find this library.
Once configured, can be used to test the following code:
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 << ""= Thread 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 }
If at compile time error [C2011 "timespec": "struct" type redefinition], because the structure is defined in [pthread.h time.h timespec and the repeated, simultaneous two different header files compiled conditions, resulting in defined repetitive structure],
solution:
#define PTHREAD_H