我有一个包含Plane结构的作业,它包含一个pthread_mutex_t
和一个pthread_cond_t
字段。但是,我不明白当我们锁定此锁或等待此条件时会发生什么。
例如:
void* landingPlane(void *ID){
int planeID = (int) ID;
plane temp;
temp.ID = planeID;
temp.requestTime = time(NULL);
pthread_mutex_init(&(temp.mutex), NULL);
pthread_cond_init(&(temp.cond), NULL);
planesArray[planeID] = temp;
int emergency_id;
pthread_mutex_lock(&runway_mutex); // this is a global mutex, I know how it works
pthread_mutex_lock(&planesArray[planeID].mutex);
// critical section
pthread_mutex_unlock(&planesArray[planeID].mutex);
pthread_mutex_unlock(&runway_mutex);
pthread_exit(NULL);
}
将互斥锁锁定在平面内的目的是什么?
答案 0 :(得分:0)
我认为这可能是一个错误。
同样,runway_mutex
被认为可以保护平面与阵列之间的导入或移除,而单个平面互斥对象则可以保护特定平面中的数据。
但这是正确的非常复杂的模式,很明显,在操作各个平面时,您绝对不会握住runway_mutex
,也不会在数组内部初始化对象互斥锁 不持有任何锁。
很难说没有看到像将平面添加到数组的代码之类的代码。