2011-10-26 37 views
0

我看到这个问题Simple example of threading in C++ 但我的问题是,我想在Windows 32中运行此程序,似乎Pthread无法识别在Windows!请告诉我什么是问题?这是我的错误: 致命错误C1010:查找预编译头时出现意外的文件结尾。 ?你忘了“#包括文件‘StdAfx.h’”添加到您的源(我加的#include“StdAfx.h中,但它仍然 不工作!)Windows线程与C++

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define NUM_THREADS 5 

void *PrintHello(void *threadid) 
{ 
    long tid; 
    tid = (long)threadid; 
    printf("Hello World! It's me, thread #%ld!\n", tid); 
    pthread_exit(NULL); 
} 

int main(int argc, char *argv[]) 
{ 
    pthread_t threads[NUM_THREADS]; 
    int rc; 
    long t; 
    for(t=0;t<NUM_THREADS;t++) 
    { 
    printf("In main: creating thread %ld\n", t); 
    rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); 
    if (rc){ 
     printf("ERROR; return code from pthread_create() is %d\n", rc); 
     exit(-1); 
     } 
    } 

    /* Last thing that main() should do */ 
    pthread_exit(NULL); 
    } 
+1

你看了这个错误吗? – SLaks

+0

我添加了#include“StdAfx.h”,但它仍然不能识别pthread! – Sara

+0

现在你包含了什么错误“StdAfx.h”? – ChrisF

回答

3

因为线程体系结构相关的概念,你将不能够使用pthread而不使用某种包装或使用特定于Windows的线程函数。

你有三种选择

  1. 使用并行线程-win32的(包装为Win32线程函数:http://sourceware.org/pthreads-win32/
  2. 使用Windows特定线程功能使用#ifdef来_WIN32的#else #ENDIF左右
  3. 使用升压包裹线程库

编辑:为C1010错误,请上面的回答。

+0

呀,用Boost.Thread – totowtwo

+0

+1的并行线程-win32的 –

2

根据你的编译器错误:

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

...问题无关与并行线程它与precompiled header files

您可以:。

  1. 在项目中使用预编译头文件关闭。项目>设置> C/C++>预编译头
  2. 做到像错误提示,并添加#include "stdafx.h"作为CPP文件的第一行
+0

,它是.cpp文件的第一行是非常重要的。 –

+0

是的,我把它放在第一线,但仍我有错误:( – Sara

+0

@约翰:对不起,我不能投你的答案,但我真的很感激:) – Sara