Re: [sipX-dev] Critical sections
Andy Spitzer wrote:
> Woof!
>
> On Wed, 01 Jul 2009 15:55:49 -0400, Dale Worley <dworley@xxxxxxxxxx> wrote:
>
>> There are two common implementations of critical sections in sipXecs:
>> OsBSem semaphore;
>> ...
>> {
>> OsLock lock(semaphore);
>> <critical section>;
>> }
>> ...
>> and
>>
>> OsMutex semaphore;
>> ...
>> semaphore.acquire();
>> <critical section>;
>> semaphore.release();
>> Is one preferable to the other?
>
> I much prefer the first one, as there is no way to exit the block without
> releasing the semaphore (the magic of C++ and auto variables)
>
In practice, I think we may get a lot of cases where we just use the parent
scope, rather than introducing a new scope nesting the critical section, e.g.
If (this == true)
{
OsLock lock(semaphore);
<critical section>;
<some-non-critical section>;
}
rather than
If (this == true)
{
{
OsLock lock(semaphore);
<critical section>;
}
<some-non-critical section>;
}
which may end up throwing obscure deadlocks down the line somewhere.
With the second implementation, even if you do forget to release the semaphore,
it's very likely you'd catch the mistake during testing.
Arjun