关于pthread_t指针转换的警告

时间:2012-08-08 04:55:41

标签: c pthreads

我制作了这个测试代码,试图将thread2的pthread_t传递给thread1,并使代码让main thread等待thread1完成而不是thread1等待thread2完成:

   void *function_thread1(void *ptr){
      pthread_t thread2;
      thread2 = (pthread_t *)ptr;
      printf("the end of the thread1\n");
      pthread_join(thread2,NULL);
      pthread_exit(0);

    }

void *function_thread2(void *ptr){
  printf("the end of the thread2\n");
  pthread_exit(0);
}

int main(void){
  pthread_t thread1,thread2;
  pthread_t *ptr2;
  ptr2 = &thread2;
  pthread_create(&thread1,NULL,function_thread2,(void*) ptr2);
  pthread_create(&thread2,NULL,function_thread1,NULL);
  printf("This is the end of main thread\n");
  pthread_join(thread1,NULL);
  exit(0);
}

它有效,但我得到以下警告,我不知道:

thread_join.c:12:10: warning: incompatible pointer to integer conversion
      assigning to 'pthread_t' (aka 'unsigned long') from 'pthread_t *'
      (aka 'unsigned long *'); dereference with *
        thread2 = (pthread_t *)ptr;
                ^ ~~~~~~~~~~~~~~~~
                  *
1 warning generated.

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你应该这样做:

pthread_t *thread2;
thread2 = ptr;

pthread_join(*thread2, NULL);