简体   繁体   English

在Objective-c中,如何确保一个代码在另一个代码之后被调用?

[英]In Objective-c how do I ensure that one code is called after another code?

I don't mean synchronize and stuff. 我的意思不是同步和东西。 This is not in general good programming practice actually. 实际上,这通常不是良好的编程习惯。 I just think that there is something that should be done. 我只是认为应该做些事情。

For example, say I have a thread that will suspend it self and wait till an event occur. 例如,假设我有一个线程将自我挂起并等待事件发生。 Often the event occur before that thread suspend itself. 通常,事件在该线程挂起之前发生。

So I want the event to wait till the thread suspend itself before it occurs. 因此,我希望事件一直等到线程挂起之前才发生。

How do I do that? 我怎么做?

For example, say I want to do 5 things on mainQueue. 例如,说我想对mainQueue做5件事。

Do something. 做一点事。 Wait till it's finished, do next. 等待直到完成,下一步。

I do this then: 然后我这样做:

-(void) vDoSomeStuffsInSequence:(NSArray *) arblArrayOfBlocksToExecuteOnMainThread
{
    if (self.blockThis) {
        PO(@"Cancel Push View Controller");
        return;
    }
    @synchronized(self)
    {
        self.blockThis=true;
    }
    AssertMainThread

    NSMutableArray * arblNotFirstBlocks = [arblArrayOfBlocksToExecuteOnMainThread mutableCopy];

    void(^blockToExecute)() = arblNotFirstBlocks[0];//execute first block right away
    blockToExecute();
    PO(@"execute pop push etc");
    [arblNotFirstBlocks removeObjectAtIndex:0];


    [[NSOperationQueue new] addOperationWithBlock:^{
        PO(@"before first suspend");
        [self vSuspendAndHaltThisThreadTillUnsuspended]; //This often happen after (instead of before) the code that execute [self vContinue is called]
        PO(@"after first suspend");
        for (void(^blockToExecute)() in arblNotFirstBlocks) {

            while(false);
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                blockToExecute();//dothisfirst must unsuspend a thread somewhere
            }];

            [self vSuspendAndHaltThisThreadTillUnsuspended];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            @synchronized(self)
            {
                self.blockThis=false;
            }
            [self vContinue];
        }];
        [self vSuspendAndHaltThisThreadTillUnsuspended];
        PO(@"finish vDoSomeStuffsInSequence");
    }];
}

What you are describing is a well-known race condition that arises in many event-delivery systems, and the solution depends on the details of the system's programming interface. 您所描述的是在许多事件传递系统中出现的众所周知的竞争条件,而解决方案取决于系统编程接口的详细信息。

For example, detecting a Unix signal is prone to this race condition, and there are a number of solutions (of varying portability). 例如,检测Unix信号很容易出现这种竞争情况,并且有许多解决方案(具有不同的可移植性)。 See “Handling Signals” on the Event Loop Wikipedia page for a couple of them. 请参阅事件循环 Wikipedia页面上的“处理信号”

So the answer to your question depends entirely on what events you want to detect and the programming interfaces available to detect them. 因此,问题的答案完全取决于您要检测哪些事件以及可用来检测它们的编程接口。 You need to edit your post to include those details if you want a more specific answer. 如果您需要更具体的答案,则需要编辑您的帖子以包括这些详细信息。

Polling, waiting, and long or indefinite thread or resource blocking are generally best avoided. 通常最好避免轮询,等待以及较长或不确定的线程或资源阻塞。 Nevertheless... you can wait using condition variables, dispatch_group_wait , or -[NSOperationQueue waitUntilAllOperationsAreFinished] . 不过,您可以使用条件变量, dispatch_group_wait-[NSOperationQueue waitUntilAllOperationsAreFinished]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM