从如下所示的代码:我不明白pthread_create函数如何将long类型的线程转换为void* 类型。我知道这是函数的格式,但是long是整数数据类型,而void* 是指针类型。
谢谢你,
int main() {
long thread;
pthread_t* thread_handles;
ready =0;
/* create an array of thread handles */
thread_handles = malloc (thread_count*sizeof(pthread_t));
/* start the threads, giving each a unique rank */
for (thread = 0; thread < thread_count; thread++)
{
pthread_create(&thread_handles[thread], NULL,
Hello, (void*) thread);
}
/* print a message from the main thread */
printf("Hello from the main thread\n");
/* wait for all threads to complete */
for (thread = 0; thread < thread_count; thread++)
{
pthread_join(thread_handles[thread], NULL);
}
/* delete the array of thread handles */
free(thread_handles);
return 0;
}
我曾经在https://www.ibm.com/docs/en/zos/2.4.0?topic=functions-pthread-create-create-thread这样的网站上做过研究,但是它没有提到背后的原因。我知道这是格式,但是铸造不应该工作?
1条答案
按热度按时间rvpgvaaj1#
指针本身是整数值,它保存内存中的地址。所以编译器将long类型转换为指针没有问题。