Windows系统下pthread环境配置

来源:互联网 发布:python hmm 编辑:程序博客网 时间:2024/05/29 03:18

记录下win7系统,vc6.0++编译器下配置POSIX多线程环境的步骤。

配置

  1. 下载地址 ftp://sourceware.org/pub/pthreads-win32/
    我下载的版本是 fpthreads-w32-2-1-0-release.exe 2005/3/16
    运行后点Exract,解压完成后再点Done,可以看到三个文件夹,但只需要用到Pre-built.2文件夹。

  2. Pre-built.2/include中的三个头文件复制到vc的Include文件夹内

  3. Pre-built.2/lib中的8个文件复制到vc的Bin文件夹内(不知道需不需要,反正移就完事了)

  4. Pre-built.2/lib中的pthreadVC2.libpthreadVSE2.lib复制到vc的Lib文件夹里(否则会出现链接错误LNK1104:cannot open file "pthreadVC2.lib"

  5. Pre-built.2/lib中的pthreadVC2.dll文件复制到C:\Windows\system里面(我的电脑中有system和system32两个文件夹,试了下发现应该放在system中)

  6. 配置完成,编程的时候需要包含pthread.h头文件,并且要加上一句#pragma comment(lib, "pthreadVC2.lib") (否则会出现链接错误)

测试

#include <cstdio>#include <pthread.h>#pragma comment(lib, "pthreadVC2.lib")void * fun(void * arg) {    printf("Hello, %s\n", (char *) arg);    return NULL;}int main(void) {    pthread_t t1, t2;    pthread_create(&t1, NULL, fun, "thread1");    pthread_create(&t2, NULL, fun, "thread2");    pthread_join(t1, NULL);    pthread_join(t2, NULL);    return 0;}

输出结果

Hello, thread1Hello, thread2