您的位置:首页 > 其它

Windows 7 64bit和Visual Studio 2010下安装及使用Pthread-w32 2.8

2013-03-13 06:33 489 查看

Windows 7 64bit和Visual Studio 2010下安装及使用Pthread-w32 2.8

Pthread是由POSIX提出的一套通用的线程库,它广泛的被各种Unix系统所支持。因此,它Unix/Linux平台下具有很好的可移植性。然而,win32平台尚未,看起来将来也不准备支持Pthread标准。为了解决这个为题,Pthread-w32提供了一个高效的win平台下的对Pthread线程库。通过使用这个库,我们可以更加方便的把win32程序移植到nix/Linux平台下。本文简单介绍了Pthread-w32的安装与使用。

官方网站:http://sourceware.org/pthreads-win32/

1).下载Windows版本的pthread。目前最新版本是:pthreads-w32-2-8-0-release.exe。主页地址:http://sourceware.org/pthreads-win32/

2).双击pthreads-w32-2-8-0-release.exe,会出现解压对话框,"browse"选择指定目录,"extract"解压,"done"完成。

3).完成后,该目录会多出三个文件夹:Pre-built.2,pthreads.2,QueueUserAPCEx。这里我们主要使用Pre-built.2,里面包含了编译好的lib和dll。我们需要的头文件和库文件也包含在这个文件夹中。pthreads.2文件夹中包含了源代码。我们也可自行编译需要的库文件。

4).新建一个win32控制台程序。命名为Pthread_Test。代码后附。

5). 在Project ->Pthread_Test Properties -> Configuration Properties-> C/C++ -> General ->Additional Include Directories 中增加头文件路径。



6). 在Project ->Pthread_Test Properties -> Configuration Properties-> Linker -> General-> Additional Library Directories 中增加库文件路径。我用的是x86库。



7). 在Project ->Pthread_Test Properties -> Configuration Properties-> Linker -> Input ->Additional Dependencies中增加所依赖的库文件。这里我们使用的IDE是VS2010,所以我们使用pthreadVSE2.lib。各个库的不同之处在pthreads.2下的README文档中有介绍。



8) 在system path中添加dll文件的路径,或者把dll加入到所在项目文件夹中。我用的是x86文件夹。

9). 编译,链接程序,测试结果如下。





9). 64位lib和dll可在此下载

https://code.google.com/p/libusb-winusb-wip/downloads/detail?name=pthread-win32_x64.zip&can=2&q=

Pthread_Test.cpp代码:

// Pthread_Test.cpp : Defines the entry point for the consoleapplication.

//

#include "stdafx.h"

#include <stdio.h>

#include <pthread.h>

#include <assert.h>

void* Function_t(void* Param);

int _tmain(int argc, _TCHAR* argv[])

{

pthread_tpid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setscope(&attr,PTHREAD_SCOPE_PROCESS);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(&pid, &attr,Function_t, NULL);

printf("====\n");

getchar();

pthread_attr_destroy(&attr);

return0;

}

void* Function_t(void* Param)

{

printf("Thread Starts.\n");

pthread_tmyid = pthread_self();

printf("Thread ID=%d ", myid);

returnNULL;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: