#import #import "lockedQueue.h" @implementation lockedQueue - (id)init { if (self = [super init]) { pthread_mutex_init(&mx, NULL); pthread_cond_init(&cv, NULL); array = [[NSMutableArray alloc] init]; } return self; } - (void)enqueue:(id)object { pthread_mutex_lock(&mx); [array insertObject:object atIndex:0]; pthread_cond_signal(&cv); pthread_mutex_unlock(&mx); } - (id)dequeue { id rv; pthread_mutex_lock(&mx); while (![array count]) { pthread_cond_wait(&cv, &mx); } rv = [[[array lastObject] retain] autorelease]; [array removeLastObject]; pthread_mutex_unlock(&mx); return rv; } - (void)clear { pthread_mutex_lock(&mx); [array removeAllObjects]; pthread_mutex_unlock(&mx); } - (void)dealloc { [array release]; [super dealloc]; } @end