2

什么相当于 iOS 中 Win32 的 WaitForMultipleObjects 函数?

这大致是我想要的:

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];

在 iOS 中实现这一点的方法是什么?

编辑:我并不一定要使用 NSCondition,任何其他同步的东西都可以(我刚刚用谷歌搜索并找到了 NSCondition)。

4

3 回答 3

1

您可以为每个具有唯一通知名称的条件创建一个 NSNotifications。然后对于每个通知都会调用相同的函数。

于 2012-05-17T11:46:11.797 回答
1

那这个呢?

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();
}

显然,这具有有效地使线程加倍的缺点,但我不知道是否有更简单的方法来实现这一点。

于 2012-05-17T12:23:13.057 回答
0

好的,我最终使用了我自己的解决方案,使用 GCD 的块通知组。我还使用了串行队列(而不是并发),它保证我们只创建 1 个额外的线程。

@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);
}
于 2012-05-21T13:25:14.843 回答