How does this recursive synchronized call not deadlock?

主宰稳场 提交于 2019-12-07 13:13:59

问题


I have a set of methods that all synchronize to the class object (can't use self, because multiple instances of this object could be used in multiple threads). Some of those methods call other methods in the class that also synchronize on the class object. Somehow this works and does not cause the deadlock I would expect it to.

I would assume that testA would be blocked from running because testB already has a lock on the class object, but this apparently isn't the case.

Is it something special that @synchronized is doing or is this a feature of the underlying mutex locks?


Example code that works:

- (NSUInteger)testA
{
    @synchronized(self.class)
    {
        NSLog(@"Doing something in A");
    }
    return 1;
}

- (void)testB
{
    @synchronized(self.class)
    {
        NSLog(@"Doing something in B");
        NSLog(@"A returned: %i", [self testA]);
    }
}

When calling testB the output is:

Doing something in B
Doing something in A
A returned: 1

回答1:


Mauricio's comment is correct. From TOCPL:

The Objective-C synchronization feature supports recursive and reentrant code. A thread can use a single semaphore several times in a recursive manner; other threads are blocked from using it until the thread releases all the locks obtained with it; that is, every @synchronized() block is exited normally or through an exception.



来源:https://stackoverflow.com/questions/8956857/how-does-this-recursive-synchronized-call-not-deadlock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!