Reputation: 6062
What is equivalent of Win32's WaitForMultipleObjects function in iOS?
This is roughly what I want:
NSCondition *condition1;
NSCondition *condition2;
NSCondition *condition3;
wait_for_conditions([NSArray arrayWithObjects: condition1, condition2, condition3, nil],
^{
// Some code which must be executed when all conditions were fired
});
// in some other places of program:
[condition1 signal];
// ...
[condition2 signal];
// ...
[condition3 signal];
What are the ways to accomplish this in iOS?
Edit: I'm not bound to usage of NSCondition, any other synchronization things will be ok (I've just googled and found NSCondition).
Upvotes: 2
Views: 1540
Reputation: 6062
OK, I ended up using my own solution using GCD's groups of blocks notifications. I've also used serial queue (instead of concurrent) which guarantees we create only 1 additional thread.
@interface WaitCondition : NSObject
- (void) fire;
@end
@implementation WaitCondition {
BOOL fired;
NSCondition *condition;
}
- (id) init
{
if ((self = [super init])) {
condition = [NSCondition new];
}
return self;
}
- (void) fire
{
[condition lock];
[condition signal];
fired = YES;
[condition unlock];
}
- (void) wait
{
[condition lock];
while (!fired) {
[condition wait];
}
[condition unlock];
}
@end
void Dispatch_NotifyForConditions(NSArray *conditions, dispatch_block_t completion)
{
dispatch_queue_t queue = dispatch_queue_create("notify_queue", NULL);
dispatch_group_t group = dispatch_group_create();
for (WaitCondition *condition in conditions)
{
NSCAssert([condition isKindOfClass:[WaitCondition class]], @"Dispatch_NotifyForConditions: conditions must contain WaitCondition objects only.");
dispatch_group_async(group, queue, ^{
[condition wait];
});
}
dispatch_group_notify(group, queue, completion);
dispatch_release(queue);
dispatch_release(group);
}
Upvotes: 0
Reputation: 55583
What about this?
void async(void (^block)())
{
[NSThread detachNewThreadSelector:@selector(invoke) toTarget:[block copy] withObject:nil];
}
__attribute__((sentinel(NULL)))
void wait_for_conditions(void (^block)(), NSCondition *condition, ...)
{
va_list args;
va_start(args, condition);
NSMutableArray *conditions = [NSMutableArray array];
do {
[conditions addObject:condition];
} while ((condition = va_arg(args, NSCondition *)));
va_end(args);
NSCondition *overallCondition = [NSCondition new];
for (int i = 0; i < conditions.count; i++) {
NSCondition *cond = [conditions objectAtIndex:i];
async(^{
[cond lock];
[cond wait];
[cond unlock];
[overallCondition lock];
[overallCondition signal];
[overallCondition unlock];
});
}
for (int i = 0; i < conditions.count; i++) {
[overallCondition lock];
[overallCondition wait];
[overallCondition unlock];
}
if (block)
block();
}
Obviously this has the drawback of effectively doubling your threads, but I don't know if there is an easier way of accomplishing this.
Upvotes: 1
Reputation: 2585
You can create a NSNotifications for each condition with unique notification name. Then for each notification will call the same function.
Upvotes: 1