为什么 pthread 返回的值与传递给线程函数的值不同?
Why the value returned by a pthread is not same as passed to the thread function?
我的理解 pthread_join manual 是我的程序应该输出与我提供给 print_xs
相同的值,但获得的输出是:
Message from printxs 11131 a
Message from printxs 11234 b
32766 -16
32766 0
程序:
#include<stdio.h>
#include<pthread.h>
typedef struct paramsThread{
char c;
int count;
} threadPara;
void* print_xs(void* unused){
threadPara *tp = (threadPara *)unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return (void*)tp;
}
int main(){
pthread_t thread1, thread2;
threadPara t1,t2,t3,t4;
t1.c = 'a';
t2.c = 'b';
t1.count = 11131;
t2.count = 11234;
t3.count=0;
pthread_create(&thread1, NULL, &print_xs, &t1);
pthread_create(&thread2, NULL, &print_xs, &t2);
pthread_join(thread1,(void*)&t3);
printf("%d %d\n", t3.count, t3.c);
pthread_join(thread2,(void*)&t4);
printf("%d %d\n", t4.count, t4.c);
return 0;
}
有人可以解释为什么会这样吗?
如果你read a pthread_join
reference or manual page你会看到第二个参数是一个指针指向一个指针。这是一种在 C 中模拟 按引用传递 的方法。
线程函数returns的指针被复制到另一个指针中。
解决方案是使用 t3
和 t4
的指针代替:
threadPara t1, t2;
threadPara *t3, *t4;
// ...
pthread_join(thread1,(void**)&t3); // Pass pointer to the pointer, emulating pass by reference
printf("%d %d\n", t3->count, t3->c);
pthread_join(thread2,(void**)&t4);
printf("%d %d\n", t4->count, t4->c);
如果您打印指针,或使用调试器,那么您将看到 t3
将指向 t1
(即 t3 == &t1
),[=13 也是如此=] 和 t2
.
线程函数returns一个地址,一个void*
。
pthread_join()
接收此地址 to 其中它的 2nd 参数 (a void**
) 分到.
所以在您的代码中使用它而不是
pthread_join(thread1, (void*)&t3);
做
{
void * pv;
pthread_join(thread1, &pv);
if (NULL != pv)
{
t3 = *((threadPara*) pv);
}
}
作为旁注:在 C 中有 no 需要转换 to/from void
-指针 from/to 其他指针,指针涉及的内容会隐式转换。
所以这段代码
void* print_xs(void* unused){
threadPara *tp = (threadPara *)unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return (void*)tp;
}
完全可以这样写:
void* print_xs(void* unused){
threadPara *tp = unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return tp;
}
我的理解 pthread_join manual 是我的程序应该输出与我提供给 print_xs
相同的值,但获得的输出是:
Message from printxs 11131 a
Message from printxs 11234 b
32766 -16
32766 0
程序:
#include<stdio.h>
#include<pthread.h>
typedef struct paramsThread{
char c;
int count;
} threadPara;
void* print_xs(void* unused){
threadPara *tp = (threadPara *)unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return (void*)tp;
}
int main(){
pthread_t thread1, thread2;
threadPara t1,t2,t3,t4;
t1.c = 'a';
t2.c = 'b';
t1.count = 11131;
t2.count = 11234;
t3.count=0;
pthread_create(&thread1, NULL, &print_xs, &t1);
pthread_create(&thread2, NULL, &print_xs, &t2);
pthread_join(thread1,(void*)&t3);
printf("%d %d\n", t3.count, t3.c);
pthread_join(thread2,(void*)&t4);
printf("%d %d\n", t4.count, t4.c);
return 0;
}
有人可以解释为什么会这样吗?
如果你read a pthread_join
reference or manual page你会看到第二个参数是一个指针指向一个指针。这是一种在 C 中模拟 按引用传递 的方法。
线程函数returns的指针被复制到另一个指针中。
解决方案是使用 t3
和 t4
的指针代替:
threadPara t1, t2;
threadPara *t3, *t4;
// ...
pthread_join(thread1,(void**)&t3); // Pass pointer to the pointer, emulating pass by reference
printf("%d %d\n", t3->count, t3->c);
pthread_join(thread2,(void**)&t4);
printf("%d %d\n", t4->count, t4->c);
如果您打印指针,或使用调试器,那么您将看到 t3
将指向 t1
(即 t3 == &t1
),[=13 也是如此=] 和 t2
.
线程函数returns一个地址,一个void*
。
pthread_join()
接收此地址 to 其中它的 2nd 参数 (a void**
) 分到.
所以在您的代码中使用它而不是
pthread_join(thread1, (void*)&t3);
做
{
void * pv;
pthread_join(thread1, &pv);
if (NULL != pv)
{
t3 = *((threadPara*) pv);
}
}
作为旁注:在 C 中有 no 需要转换 to/from void
-指针 from/to 其他指针,指针涉及的内容会隐式转换。
所以这段代码
void* print_xs(void* unused){
threadPara *tp = (threadPara *)unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return (void*)tp;
}
完全可以这样写:
void* print_xs(void* unused){
threadPara *tp = unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return tp;
}