0、pthreadソースのダウンロード:https : //sourceware.org/pthreads-win32/
1. pthreads-w32-2-9-1-release.zipをダウンロードした後、解凍すると、内容は次のようになります。
このうち、[Pre-built.2]は、win32のpthreadのヘッダーファイルとライブラリファイル、[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 << "<< myid.x << std :: endl; 14 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は 0を返し ます。 29 }
コンパイル中にエラー[C2011 "timespec": "struct" type redefinition]が報告された場合、その理由は、timespecの構造定義とpthread.hのtime.hが繰り返され、2つのヘッダーファイルのコンパイル条件が異なるためです。構造の繰り返し定義]、
解決策:
#define PTHREAD_H
#pragma comment(lib、 "x86 / pthreadVC2.lib")