【发布时间】:2014-11-21 03:17:50
【问题描述】:
我正在尝试创建多个线程并将不同的值传递给每个线程以解决餐饮哲学家的问题。但是我收到了这个错误:
warning: cast to pointer from integer of different size
这是我的代码:
pthread_mutex_t mutex;
pthread_cond_t cond_var;
pthread_t philo[NUM];
int main( void )
{
int i;
pthread_mutex_init (&mutex, NULL);
pthread_cond_init (&cond_var, NULL);
//Create a thread for each philosopher
for (i = 0; i < NUM; i++)
pthread_create (&philo[i], NULL,(void *)philosopher,(void *)i); // <-- error here
//Wait for the threads to exit
for (i = 0; i < NUM; i++)
pthread_join (philo[i], NULL);
return 0;
}
void *philosopher (void *num)
{
//some code
}
【问题讨论】:
-
pthread_create() 采用指针而不是 int。
-
我改变了 pthread_create (&philo[i], NULL,(void *)philosopher,(void *)i);到 pthread_create (&philo[i], NULL,(void *)philosopher,(int *)i);还是不行.....
-
为什么要将
philosopher转换为void *?那是错误的类型。 -
@RADAR:这里绝对不能传递
i的地址。当那些线程试图访问它时,i的值将是完全不可预测的,因为你只有一个变量,它正在改变,并且对它的访问是不同步的。它只会持续存在的事实不是问题。 -
@Paul Griffiths,你是对的
标签: c pthreads dining-philosopher