Pthread代码在linux机器上运行良好,但在使用eclipse的Windows上运行不正常

时间:2017-05-08 11:03:56

标签: c++ linux windows eclipse pthreads

演示简单的thread.Code在linux操作系统上运行完美,但无法在windows上运行。我在eclipse中添加了所需的库文件.exe在最后崩溃。我已经尝试了与linux相同的代码,它工作得非常好。我正在使用带有pthread库的c ++使用eclipse来解决这个问题。

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <exception>

using namespace std;

#define NUM_THREADS     5

struct thread_data{
   int  thread_id;
   char *message;
};

void *PrintHello(void *threadarg) {
   struct thread_data *my_data;
   try
   {

   my_data = (struct thread_data *) threadarg;

   cout << "Thread ID : " << my_data->thread_id ;
   cout << " Message : " << my_data->message << endl;

   pthread_exit(NULL);
   throw "error in function";
   }
   catch(const char* msg)
   {
       cerr << msg << endl;
   }
}

int main () {
   pthread_t threads[NUM_THREADS];
   struct thread_data td[NUM_THREADS];

   int rc;
   int i;
   try
   {

   for( i=0; i < NUM_THREADS; i++ ){
      cout <<"main() : creating thread, " << i << endl;
      td[i].thread_id = i;
      td[i].message = "This is message";
      rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]);

      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }

   pthread_exit(NULL);
   throw "error in Main function";

   }
   catch(const char* msg)
   {
       cerr << msg << endl;
   }

}

0 个答案:

没有答案