Windows下使用pthread-開發(fā)環(huán)境搭建 (qq.com)
一.Windows下使用pthread-開發(fā)環(huán)境搭建

1.1 下載源碼
1.https://sourceforge.net/projects/pthreads4w/files/ 比官網(wǎng)新一點(diǎn)2018年 3.0版本
2.https://sourceware.org/pthreads-win32/ 官網(wǎng) 最新2012年 2.9.1版本
ftp://sourceware.org/pub/pthreads-win32/ 源碼下載
https://sourceware.org/pthreads-win32/manual/index.html API參考
3.https://github.com/GerHobbelt/pthread-win32 適配了MSVC的版本
1.2 庫編譯
使用上述第3個(gè)資源,因?yàn)镸SVC編譯環(huán)境都適配好了。
這里使用MSVC2022
打開pthread-win32\\windows\\VS2022\\pthread.2022.sln,
有三個(gè)工程分別是,生成動態(tài)鏈接庫dll,靜態(tài)鏈接庫lib和測試的工程。

點(diǎn)擊左側(cè)目錄,解決方案’pthread.2022’
菜單欄點(diǎn)擊 生成->生成解決方案 開始構(gòu)建

生成的dll和lib位于pthread-win32\\windows\\VS2022\\bin\\Debug-Unicode-64bit-x64下
其中
動態(tài)鏈接庫使用
pthread.dll
pthread.dll
靜態(tài)鏈接庫使用
pthread_static_lib.lib
1.3 測試
在解決方案目錄,右鍵點(diǎn)擊屬性

修改啟動項(xiàng)目

然后點(diǎn)擊如下圖標(biāo)運(yùn)行

pthread-win32\\tests\\wrapper4tests_1.c中測試用例
TEST_WRAPPER(test_sequence2);會失敗
先注釋掉該用例。
看到測試結(jié)果如下:

1.4 在自己工程中使用
1.4.1 使用靜態(tài)鏈接庫
新建空白WIN32程序
將上述的
pthread.dll
pthread.lib
pthread_static_lib.lib
復(fù)制到工程目錄Src/pthread/lib下

將源碼pthread-win32下的所有.h文件復(fù)制到
復(fù)制到工程目錄Src/pthread/inc下

右鍵點(diǎn)擊工程名->屬性

設(shè)置Lib文件夾路徑
$(MSBuildProjectDirectory)\\Src\\pthread\\lib;

設(shè)置lib文件

設(shè)置頭文件包含路徑$(MSBuildProjectDirectory)\\Src\\pthread\\inc;

添加源文件main.c,內(nèi)容如下
創(chuàng)建兩個(gè)線程,分別延時(shí)不同時(shí)間。
#include < stdio.h >
#include < pthread.h >
static void* thread1(void* arg)
{
const struct timespec interval = { 1L, 500000000L };
while (1)
{
pthread_delay_np(&interval);
printf("thread1\\r\\n");
}
return 0;
}
static void* thread2(void* arg)
{
const struct timespec interval = { 3L, 0L };
while (1)
{
pthread_delay_np(&interval);
printf("thread2\\r\\n");
}
return 0;
}
int main(void)
{
pthread_t t1;
pthread_t t2;
pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);
while (1);
}
構(gòu)建項(xiàng)目,然后運(yùn)行
可以看到基本是thread1運(yùn)行兩次thread運(yùn)行1次,和其delay時(shí)間是兩倍關(guān)系對應(yīng)。

使用靜態(tài)鏈接庫編譯的話exe文件可直接運(yùn)行。
1.4.2 使用動態(tài)鏈接庫
與靜態(tài)鏈接時(shí)一樣
只是配置鏈接的庫文件是pthread.lib

運(yùn)行時(shí)需要將exe文件和pthread.dll放在一起。
審核編輯:湯梓紅
-
嵌入式
+關(guān)注
關(guān)注
5208文章
20620瀏覽量
336672 -
WINDOWS
+關(guān)注
關(guān)注
4文章
3705瀏覽量
94301 -
開發(fā)環(huán)境
+關(guān)注
關(guān)注
1文章
275瀏覽量
17670 -
環(huán)境搭建
+關(guān)注
關(guān)注
0文章
60瀏覽量
9488
發(fā)布評論請先 登錄
搭建C語言開發(fā)環(huán)境(Windows平臺)匯總
php開發(fā)環(huán)境的搭建和使用
ESP32教程——Windows開發(fā)環(huán)境搭建.pdf下載
如何在Windows系統(tǒng)下安裝搭建PHP環(huán)境
ESP32教程之如何在Windows系統(tǒng)上搭建開發(fā)環(huán)境
MM32F013x——Windows環(huán)境下基于Eclipse開發(fā)、調(diào)試MM32
MM32F013x——Windows下搭建Eclipse開發(fā)環(huán)境
MM32F013x——Windows下搭建Eclipse開發(fā)環(huán)境
Windows下使用pthread-開發(fā)環(huán)境搭建
評論