gpt4 book ai didi

c - 关于 pthread_t 指针转换的警告

转载 作者:行者123 更新时间:2023-11-30 15:55:42 28 4
gpt4 key购买 nike

我编写此测试代码是为了尝试将线程2的pthread_t传递给线程1,并编写了让主线程等待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.

有什么想法吗?

最佳答案

你应该这样做:

pthread_t *thread2;
thread2 = ptr;

pthread_join(*thread2, NULL);

关于c - 关于 pthread_t 指针转换的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857874/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com