pthread-win32在VC2005下的使用
pthread-win32是一個在Win32環境下的Unix POSIX線程庫的移植. 有了它, 可以比較方便的移植Unix/Linux多線程程序到Windows下. 在VC2005下使用也很簡單:
下載, 地址是?http://sourceware.org/pthreads-win32
里面include目錄中是頭文件, lib目錄中是.lib和.dll文件.
在VC項目的屬性中, 分別加入.h文件的目錄, 和.lib文件的目錄, 并在link選項中加入.lib庫:
?
寫一段最簡單的代碼來測試(創建一個線程, 輸出一點東西):
#include "stdafx.h"
#include <pthread.h>
?
struct dt {
????int a;
????int b;
};
?
void* thread_function(void* arg);
?
int _tmain(int argc, _TCHAR* argv[])
{
????int result0;
????dt data0;
????data0.a = 0;
????data0.b = 1;
?
????pthread_t a_thread;
????result0 = pthread_create(&a_thread, NULL, thread_function, (void*)&data0);
????if (result0 != NULL) {
????????printf("error creating thread0!");
????????return 1;
????}
????getchar();
}
?
void* thread_function(void* arg) {
????dt* data = (dt*)arg;
????printf("%d %d", data->a, data->b);
????return NULL;
}
?
?
編譯, 鏈接, 運行, OK.
?
注意: 需要把pthreadvc2.dll copy到Debug或者Release目錄, 否則可能會出現找不到dll的錯誤.
?
另外pthread2提供了不同的.lib以及相對應的.dll,
?
????pthread[VG]{SE,CE,C}c.dll
pthread[VG]{SE,CE,C}c.lib
?
含義為
?
????[VG] 編譯器種類
V????- MS VC, or
G????- GNU C
?
????{SE,CE,C} 異常處理模式
SE????- Structured EH, or
CE????- C++ EH, or
C????- no exceptions - uses setjmp/longjmp
c????- DLL compatibility number indicating ABI and API
compatibility with applications built against
any snapshot with the same compatibility number.
See 'Version numbering' below.
?
???比如上面用的pthreadvc2.dll, 含義為:
?
???v = MSVC
???c = 沒有使用異常機制, 而是使用setjump/longjmp
???2 = 兼用性 - 和pthread2兼容, 不和舊版本pthread1兼容.
?
???詳細請參照pthread的readme.
總結
以上是生活随笔為你收集整理的pthread-win32在VC2005下的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 换颗非原厂螺丝就无法终身质保?比亚迪回应
- 下一篇: 日本版GPS助力自动驾驶:行驶不用摄像头