我正在創建一個阻塞隊列,由大約10個工作線程同時訪問。基本實現隊列是這樣的:NSCondition:遞歸鎖定?
-(void) enqueue:(__strong id)value
{
[_mutex lock];
while ([self size] == _maxSize) {
[_mutex wait];
}
[_queue enqueue:value];
[_mutex signal];
[_mutex unlock];
}
-(id) dequeue
{
[_mutex lock];
while ([self isEmpty]) {
[_mutex wait];
}
id value = [_queue dequeue];
[_mutex broadcast];
[_mutex unlock];
return value;
}
凡_mutex
是NSCondition
。這些問題都與-isEmpty
和-size
方法:
-(int) size
{
@try {
[_mutex lock];
return [_queue size];
}
@finally {
[_mutex unlock];
}
}
-(BOOL) isEmpty
{
@try {
[_mutex lock];
return [_queue isEmpty];
}
@finally {
[_mutex unlock];
}
}
因爲他們需要互斥鎖,以確保沒有數據損壞的很到位,它把程序陷入僵持,爲NSCondition
不遞歸方式鎖定。但是,如果我改變我的實現以下幾點:
-(void) enqueue:(__strong id)value
{
while ([self size] == _maxSize) {
[_mutex lock];
[_mutex wait];
[_mutex unlock];
}
[_mutex lock];
[_queue enqueue:value];
[_mutex signal];
[_mutex unlock];
}
-(id) dequeue
{
while ([self isEmpty]) {
[_mutex lock];
[_mutex wait];
[_mutex unlock];
}
[_mutex lock]; // when I require the lock here, another thread has already dequeued the object
id value = [_queue dequeue];
[_mutex broadcast];
[_mutex unlock];
return value;
}
然後程序不僵局,然而,我的時間重新AQUIRE鎖,另一名工人已經離隊,我需要已經是對象。任何想法如何使NSCondition
遞歸?
我沒有想到這一點。感謝您在盒子外面思考! – 2012-02-03 15:54:20