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库:

pthread-win32在VC2005下的使用

pthread-win32在VC2005下的使用



pthread-win32在VC2005下的使用 

写一段最简单的代码来测试(创建一个线程, 输出一点东西):

#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] 编译器种类
   - MS VC, or
   - GNU C

 

    {SE,CE,C} 异常处理模式
SE    - Structured EH, or
CE    - C++ EH, or
   - no exceptions - uses setjmp/longjmp

   - 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值