我目前正在运行此循环以关闭我的所有"工厂"然而,执行没有到达pthread_join()
之后的print语句。我的理解是,如果线程已经终止,那么pthread_join()
将立即返回。我怀疑factory_thread挂了
stop_thread_factories = true;
for(int i=0; i<factories; i++) {
printf("stopping Candy factory %d\n",i);
pthread_join(*factoryThreads[i], NULL);
printf("Candy factory %d stopped!\n",i);
//unction waits for the thread specified by thread to terminate.
}
这是工厂线程功能,我已经添加了打印语句,以查看它被卡住的位置。
void* factory_thread(void* arg){
int i = (intptr_t)arg;
int wait_time;
while(stop_thread_factories == false){
wait_time = rand()%4; //wait times will be from 0 to 3
printf("\tFactory %d ships candy & waits %ds\n", i, wait_time);
//following steps in the outline in the assignment
candy_t* newCandy = malloc(sizeof(candy_t));
newCandy->time_stamp_in_ms = current_time_in_ms();
newCandy->factory_number = i;
printf("about to insert\n");
bbuff_blocking_insert(newCandy);
printf("done inserting\n");
stats_record_produced(i);
sleep(wait_time);
}
printf("Candy-factory %d done\n", i);
pthread_exit(NULL);
printf("Just finished the exit call\n");
return NULL;
}
然而,这是我留下的输出,程序挂起在下一行。任何建议或提示将不胜感激!顺便说一句,这是为了学校作业。
stopping Candy factory 0
Candy-factory 0 done