0
如果两个线程在锁上被阻塞,你如何选择哪个get?给互斥正确的线程
例如thread1
调用mutex_lock(aMut)
但aMut
已锁定所以thread1
块。沿着thread2
,它调用aMut
上的互斥锁,它会阻塞。如何确保aMut
给予thread2
?
,这里是我的尝试不起作用
/*global variables*/
pthread_mutex_t aMut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_var = PTHREAD_COND_INITIIALIZER;
pthread_cond_t changeNumPrior = PTHREAD_COND_INITIIALIZER;
int highPriorityThreads = 0;
/*function that is passed to pthread_create()*/
void* functionThatIsPassedToPthreadCreate(void* arg)
{
bool highPriority = false;
if(arg->someConditionIsMet)
{
highPriorityThreads++;
highPriority = true;
}
pthread_mutex_lock(&changeNumPrior);
while(!highPriority && highPriorities > 0)//this doesn't work
{
pthread_cond_wait(&cond_var, &changeNumPrior);
}
pthread_mutex_unlock(&changeNumPrior);
pthread_mutex_lock(&aMut);//if this blocks, by the time this mutex is aquired, there now may be high priority threads
//code that requires exclusive access to shared resource protected by aMut
pthread_mutex_unlock(&aMut);
pthread_cond_signal(&cond_var);
}
更改您的设计,以避免此需求。 – 2014-10-27 11:51:33