日期:2014-05-16  浏览次数:20808 次

线程创建出错了
void process(char *buf)
{
  printf("****---> %s\n" , buf) ;//出错了
  int low = 0 ;
  int high = strlen(buf) - 1;
  sleep(1) ;
}

int main()
{
  FILE *srcfp , *destfp ;
  static int num = 0 ; ///add up numbers of rows
  char buffer[MAXSIZ];
  //char *buffer = malloc(MAXSIZ*sizeof(char)) ;

  void process(char* buf) ;

  if((srcfp = fopen("srcfile" , "r")) == NULL)
  {
  printf("open srcfile error!\n") ;
  exit(1) ;
  }

  if((destfp = fopen("destfile" , "w+")) == NULL)
  {
  printf("open or create destfile error!\n") ;
  exit(1) ;
  }


  while((fgets(buffer , MAXSIZ , srcfp)) != NULL)
  {
  pthread_t threadfd , thread_fd;
  num++ ;
  printf("%d %s\n" , num ,buffer) ;
  buffer[strlen(buffer)-1] = '\0' ;
  thread_fd = pthread_create(&threadfd ,NULL, (void *)(&process ), (void*)buffer) ;//这里好像没有穿过去buffer
  if(thread_fd == -1 )
  {
  printf("create pthread failed!\n") ;
  exit(1) ;
  }
  }
}

------解决方案--------------------
void* process(void *buf)
{
printf("****---> %s\n" , (char *)buf) ;//出错了
int low = 0 ;
int high = strlen(buf) - 1;
sleep(1) ;
}

main函数中:void process(char* buf) ;--这个函数声明去掉

thread_fd = pthread_create(&threadfd ,NULL, process , (void*)buffer) ;
------解决方案--------------------
thread_fd = pthread_create(&threadfd ,NULL, (void *)(&process ), (void*)buffer) ;//这里好像没有穿过

改成
thread fd = pthread_create(&htreadfd,NULL,process,(void *)buffer);//
------解决方案--------------------
void process(char *buf)
---->>>> void process(void *buf)