Windows下使用pthread

1.下载pthreads-w32-2-8-0-release.exe

下载地址:ftp://sourceware.org/pub/pthreads-win32


2. 安装pthreads-w32-2-8-0-release.exe

       双击pthreads-w32-2-8-0-release.exe,点击Browse选择安装到的目录,然后点击Extract解压,完成后点击Done。

       之后会在安装目录看到有三个文件夹Pre-built.2、pthreads.2、QueueUserAPCEx.

第一个是生成库(头文件和库文件那些),第二个是源码,第三个不清楚,像是测试程序。

       将Pre-built.2文件夹下的include和lib文件夹里的文件复制到VS2008对应的include和lib目录,我这里是C:\Program Files\Microsoft Visual Studio 9.0\VC\include和C:\Program Files\Microsoft VisualStudio 9.0\VC\lib.

 

3. 编写测试程序

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<pthread.h>  
  3. #include<Windows.h>  

  4. #pragma comment(lib, "pthreadVC2.lib")  //必须加上这句  
  5.    
  6. void*Function_t(void* Param)  
  7. {  
  8.      pthread_t myid = pthread_self();  
  9.      while(1)  
  10.      {  
  11.          printf("线程ID=%d \n", myid);  
  12.          Sleep(4000);  
  13.      }  
  14.      return NULL;  
  15. }  
  16.    
  17. int main()  
  18. {  
  19.      pthread_t pid;  
  20.      pthread_create(&pid, NULL, Function_t,NULL);  
  21.      while (1)  
  22.      {  
  23.          printf("in fatherprocess!\n");  
  24.          Sleep(2000);  
  25.      }  
  26.      getchar();  
  27.      return 1;  
  28. }  


       和Linux的pthread一样~直接编译运行就ok了。如果运行提示缺少pthreadVC2.dll,就将Pre-built.2\lib目录下的pthreadVC2.dll拷贝到可执行文件所在目录就行了。

1.下载pthreads-w32-2-8-0-release.exe

下载地址:ftp://sourceware.org/pub/pthreads-win32


2. 安装pthreads-w32-2-8-0-release.exe

       双击pthreads-w32-2-8-0-release.exe,点击Browse选择安装到的目录,然后点击Extract解压,完成后点击Done。

       之后会在安装目录看到有三个文件夹Pre-built.2、pthreads.2、QueueUserAPCEx.

第一个是生成库(头文件和库文件那些),第二个是源码,第三个不清楚,像是测试程序。

       将Pre-built.2文件夹下的include和lib文件夹里的文件复制到VS2008对应的include和lib目录,我这里是C:\Program Files\Microsoft Visual Studio 9.0\VC\include和C:\Program Files\Microsoft VisualStudio 9.0\VC\lib.

 

3. 编写测试程序

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<pthread.h>  
  3. #include<Windows.h>  

  4. #pragma comment(lib, "pthreadVC2.lib")  //必须加上这句  
  5.    
  6. void*Function_t(void* Param)  
  7. {  
  8.      pthread_t myid = pthread_self();  
  9.      while(1)  
  10.      {  
  11.          printf("线程ID=%d \n", myid);  
  12.          Sleep(4000);  
  13.      }  
  14.      return NULL;  
  15. }  
  16.    
  17. int main()  
  18. {  
  19.      pthread_t pid;  
  20.      pthread_create(&pid, NULL, Function_t,NULL);  
  21.      while (1)  
  22.      {  
  23.          printf("in fatherprocess!\n");  
  24.          Sleep(2000);  
  25.      }  
  26.      getchar();  
  27.      return 1;  
  28. }  


       和Linux的pthread一样~直接编译运行就ok了。如果运行提示缺少pthreadVC2.dll,就将Pre-built.2\lib目录下的pthreadVC2.dll拷贝到可执行文件所在目录就行了。

猜你喜欢

转载自blog.csdn.net/u012422855/article/details/78794324