1、准备工作
pthread-win32的下载地址: https://sourceware.org/pthreads-win32/#download
2、编译pthread-win32静态库
(1)、解压下载后的压缩件,解压后,有“Pre-built.2”、“pthreads.2”两个文件夹,源码就在pthreads.2文件夹下。
(2)、用vs2010打开pthreads.2下面的工程“pthread.dsw”
(3)、vs工程打开后,打开项目的属性页-》配置属性-》常规-》配置类型-》静态库(.lib)
(4)、vs工程打开后,打开项目的属性也-》配置属性-》C/C++-》预处理器-》预处理定义添加:PTW32_STATIC_LIB
(5)、在使用phread-win32静态库的项目中,项目属性-》配置属性-》C/C++-》预处理器-》预处理定义也要添加:PTW32_STATIC_LIB
3、测试用例
#include<stdio.h>
#include<pthread.h>
#include<Windows.h>
void*Function_t(void* Param)
{
pthread_t myid = pthread_self();
while(1)
{
printf("线程ID=%d \n", myid);
Sleep(4000);
}
return NULL;
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, Function_t,NULL);
while (1)
{
printf("in fatherprocess!\n");
Sleep(2000);
}
getchar();
return 1;
}
参考网址:http://blog.csdn.net/fksec/article/details/41517953