【发布时间】:2011-06-29 16:57:27
【问题描述】:
我正在尝试将 2 个无符号整数传递给 C 中新创建的线程(使用 pthread_create()),但 2 个整数的数组或结构似乎也不起作用。
// In my socket file
struct dimension {
unsigned int width;
unsigned int height;
};
unsigned int width, height;
void setUpSocket(void* dimension) {
struct dimension* dim = (struct dimension*) dimension;
width = dim->width;
height = dim->height;
printf("\n\nWidth: %d, Height: %d\n\n", width, height);
}
// In main.cpp
// Pass a struct in pthread_create
struct dimension dim;
dim.width = w;
dim.height = h;
pthread_create(&ph, &attr, (void * (*)(void *)) setUpSocket, (void *) &dim);
在调用 pthread_create 之前,dim.width 和 dim.height 是正确的。在我的socket文件中,只设置了宽度,高度为0,不明白为什么。
请问有谁知道问题出在哪里以及如何解决?
非常感谢。
【问题讨论】:
-
你的线程应该返回
void *而不是void。所以你可以停止做我不明白的那种时髦的类型转换:P
标签: c multithreading pthreads