欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

pthread-win32在VC2005下的使用

发布时间:2023/12/10 c/c++ 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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库:







 

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

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

 

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

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

总结

以上是生活随笔为你收集整理的pthread-win32在VC2005下的使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。