【发布时间】:2016-06-27 10:36:15
【问题描述】:
我目前正在创建线程,我想打印出我创建的每个线程的“线程编号”。例如:
void* thread_function(void* arg){
printf("This is thread # %d\n", *(int*)arg);
return NULL;
}
pthread_t id;
int main()
for(int i = 0; i< 5; i++){
pthread_create(&id, NULL, thread_function, &i);
}
//do some other stuff
return 0;
}
所以基本上,对于第一个线程,我想说:
This is thread # 0
但是,由于某种原因,它给了我像
这样的随机数This is thread # -56465645645
我该如何解决这个问题?
【问题讨论】:
标签: c pthreads printf void-pointers