pthread 转帖
最近打算尝试写一些跨平台的程序,本想自己封装windows下的CreateThread和linux下的pthread,后来查查资料,发现Linux社区早就提供了
windows下的pthread库,和linux下一模一样,爽。
windows下的pthread库叫做:pthreads-win32,官方网站是:http://sourceware.org/pthreads-win32/,官方FTP是:
ftp://sources.redhat.com/pub/pthreads-win32/。
郁闷的是,FTP里面的内容比较乱,部分已经编译的库有问题。我下载了一个看起来比较新的库,结果弄了半天不能链接。建议大家下载:
ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-7-0-release.exe这个自解压文件,压缩包里的pthreads.2目录是源码,
Pre-built.2目录是编译所需的头文件和库文件。
先把pthreads-w32-2-7-0-release.exe解压缩到c:/pthreads-w32-2-7-0-release文件夹。
OK!现在写一个简单的pthread来测试:
#include < stdio.h >
#include < pthread.h >
#include < assert.h >
void * Function_t( void * Param)
... {
printf( " 我是线程! " );
pthread_t myid = pthread_self();
printf( " 线程ID=%d " , myid);
return NULL;
}
int main()
... {
pthread_t pid;
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( " ======================================== " );
getchar();
pthread_attr_destroy( & attr);
return 1 ;
}
OK!使用微软的CL来编译:
rem cl.bat
cl.exe main.cpp /c /I"c:/pthreads-w32-2-7-0-release/Pre-built.2/include"
link.exe /out:main_cl.exe main.obj /LIBPATH:"c:/pthreads-w32-2-7-0-release/Pre-built.2/lib" pthreadVC2.lib
pause
或者使用GCC来编译:
rem gcc.bat
g++.exe -o main.o -c main.cpp -I"c:/pthreads-w32-2-7-0-release/Pre-built.2/include"
g++.exe -o main_gcc.exe main.o "c:/pthreads-w32-2-7-0-release/Pre-built.2/lib/libpthreadGC2.a"
pause
嘿嘿!开源就是好啊,跨平台实现得如此容易。
如何静态链接pthreads-win32
[转]http://blog.chenfeisoft.com/post/e5a682e4bd95e99d99e68081e993bee68ea5pthreads-win32.aspx
pthreads-win32是windows下的pthread库,它默认采用的是动态链接库的链接方式,因此在使用该库的程序都需要带上一个动态库pthreadVC2.dll,感觉挺不方便的,下面介绍如何静态链接pthreads-win32:
首先要编译静态库: 从ftp://sources.redhat.com/pub/pthreads-win32/下载最新的库安装包,笔者下载的是pthreads-w32-2-8-0-release.exe,自解压到一个目录,用vc7打开pthreads.2目录下的pthread.dsw,会提示工程版本转换,选择全是,然后打开该工程的属性页,在“常规”选项页的配置类型选择“静态库(.lib)”,在“c/c++”选项页的预处理器定义删除_USRDLL和PTW32_BUILD,添加PTW32_STATIC_LIB,确定保存即可。当然,你需要根据你的需要选择运行时库的类型。最后重新生成pthread便可生成我们需要的pthread.lib。
下面讲述如何使用前面生成的静态库:新建一个控制台工程,将pthread.lib拷贝到工程目录,在预处理器定义中添加PTW32_STATIC_LIB,附加包含目录添加pthreads-win32代码所在目录,笔者是E:/pthreads/pthreads.2,附加依赖项添加Ws2_32.lib和pthread.lib即可,简单使用代码如下:
#include <iostream>
#include "pthread.h"
void * Function_t(void * Param)
{
std::cout << "我是线程!" << std::endl;
pthread_t myid = pthread_self();
printf( "线程ID = %d", myid );
return NULL;
}
int main()
{
#ifdef PTW32_STATIC_LIB
pthread_win32_process_attach_np();
#endif
pthread_t pid;
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);
std::cout << "========================================" << std::endl;
getchar();
pthread_attr_destroy(&attr);
#ifdef PTW32_STATIC_LIB
pthread_win32_process_detach_np();
#endif
return 1;
}
其中
#ifdef PTW32_STATIC_LIB
pthread_win32_process_attach_np();
#endif
#ifdef PTW32_STATIC_LIB
pthread_win32_process_detach_np();
#endif
对于静态链接方式非常重要,如果没有这段代码,线程将创建失败。