0

我制作了这个测试代码来尝试将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.

有任何想法吗?

4

1 回答 1

1

你应该这样做:

pthread_t *thread2;
thread2 = ptr;

pthread_join(*thread2, NULL);
于 2012-08-08T04:57:32.187 回答