多線程編程之VC6.0安裝pthread
多線程是C/C++的一個重要的概念,在windows下,需要安裝pthread來支持多線程編程。以下配置本人親測成功,另外此次配置實際上與編譯器無關,故其他編譯器如VS2010等可參照本人配置。
1.下載pthreads-w32-2-8-0-release.zip:
2.解壓pthreads-w32-2-8-0-release.zip:
解壓後如下:
3.運行pthreads-w32-2-8-0-release.exe:
彈出的對話框點擊:Extract,結束後點擊Done完成。運行後如下:
我們需要的文件夾是Pre-built.2,另外兩個文件夾不需要。
3.拷貝include內文件:
點開Pre-built.2文件夾:
將include內的3個文件(如下):
將這3個文件複製到VC6.0對應的include文件夾內:我的路徑是:E:\Mysoftware\VC98\Include
4.拷貝bin內文件:
點開include下面的lib文件夾(含10個文件如下):
將這10個文件複製到VC6.0對應的bin文件夾內:我的路徑是:E:\Mysoftware\VC98\Bin
5.將pthreadVC2.dll添加到系統:
如果用Win8,將上面bin文件中的pthreadVC2.dll複製到:C:\Windows\System
其他Windows操作系統,則複製到:C:\Windows\System32(注Win8最好也複製一個到這個文件夾,可以提供32位程序支持)。
6.測試程序:
#include "pthread.h"
#include "sched.h"
#include "semaphore.h"
#include "stdio.h"
#pragma comment(lib, "pthreadVC2.lib") //必須加上這句
void* tprocess1(void* args)
{
int i = 1;
while(i <= 100)
{
printf("process1:%d\n", i);
i++;
}
return NULL;
}
void* tprocess2(void* args)
{
int i = 1;
while(i <= 100)
{
printf("process2:%d\n", i);
i++;
}
return NULL;
}
int main()
{
pthread_t t1;
pthread_t t2;
pthread_create(&t1,NULL,tprocess1,NULL);
pthread_create(&t2,NULL,tprocess2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
讀者只需運行一下這個測試程序即可,以後會學習關於Pthread的更多知識。
這便是線程1和線程2交替運行的結果啦!NICE!
再次提醒一下哦,此次配置與編譯器無關,其他編譯器也可參照配置哦。開始多線程編程之旅吧!
來自 <https://blog.csdn.net/lovecodeless/article/details/22751299>