天天看點

windows 下使用pthread

環境:  Window     VS 2010      

1 先去下載下傳pthread庫 ​​ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-8-0-release.exe​​

2 解壓exe檔案,将Pre-built.2目錄下的include 和lib拷貝到工程目錄下

3 VS設定附加包含目錄--include,附加庫目錄--lib

4 測試代碼

#include <stdio.h>
#include <iostream>
#include "include\pthread\pthread.h" // 添加 pthread庫
#pragma comment(lib, "pthreadVC2.lib")//不可缺少,否則編譯報錯

using namespace std;

void *PthreadFun(void *arg)
{
  int i =1;
  while(i)
  {
    cout<<i<<endl;
    i++;
  }
  return arg;
}

int main(int argc,char *argv[])
{
  pthread_t pid;
  pthread_create(&pid,NULL,PthreadFun,NULL);
  system("pause");
}