From: Alexander Grigoriev on

"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
news:d3e3645od2dfcbs5dcjh5aohclr7ghabn5(a)4ax.com...
> Some questions, see below...
>
>>> SecAtt.nLength=sizeof(SECURITY_DESCRIPTOR);
>>> if ((g_hSem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,"Global\
>>> \MyApp"))==NULL)
>>> {
>>> g_hSem = CreateSemaphore(&SecAtt,3,3,"Global\\MyApp");
>>> }
>>>
> ****
> In the above sequence, suppose I have two sequences in thread A and B that
> can be
> represented as o (open) and c (create), where ~ means the operation fails
>
>
>
> if the sequences are
>
> Ao~Bo~AcBc
> Ao~Bo~BcAc
>
> Why the OpenSemaphore call at all? Why not just CreateSemaphore?
> CreateSemaphore says if
> the named semaphore already exists, it applies SEMAPHORE_ALL_ACCESS, which
> is all that is
> happening here.
>
> So what has been gained here? Note the window between open and create
> allows the other
> thread to create it before the first thread gets to create it.

There is a subtle problem with Create<object> (addressed in Vista, IIRC),
that if the object already exists, the OS will try to open it with
ALL_ACCESS, which may not be granted, though it doesn't apply to this
example. This is why it makes sense to try Open<object> first.


From: Joseph M. Newcomer on
See below...
On Wed, 25 Jun 2008 07:55:41 -0700, "Alexander Grigoriev" <alegr(a)earthlink.net> wrote:

>
>"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>news:d3e3645od2dfcbs5dcjh5aohclr7ghabn5(a)4ax.com...
>> Some questions, see below...
>>
>>>> SecAtt.nLength=sizeof(SECURITY_DESCRIPTOR);
>>>> if ((g_hSem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,"Global\
>>>> \MyApp"))==NULL)
>>>> {
>>>> g_hSem = CreateSemaphore(&SecAtt,3,3,"Global\\MyApp");
>>>> }
>>>>
>> ****
>> In the above sequence, suppose I have two sequences in thread A and B that
>> can be
>> represented as o (open) and c (create), where ~ means the operation fails
>>
>>
>>
>> if the sequences are
>>
>> Ao~Bo~AcBc
>> Ao~Bo~BcAc
>>
>> Why the OpenSemaphore call at all? Why not just CreateSemaphore?
>> CreateSemaphore says if
>> the named semaphore already exists, it applies SEMAPHORE_ALL_ACCESS, which
>> is all that is
>> happening here.
>>
>> So what has been gained here? Note the window between open and create
>> allows the other
>> thread to create it before the first thread gets to create it.
>
>There is a subtle problem with Create<object> (addressed in Vista, IIRC),
>that if the object already exists, the OS will try to open it with
>ALL_ACCESS, which may not be granted, though it doesn't apply to this
>example. This is why it makes sense to try Open<object> first.
>
****
But, under the scenario I show above, the Create *would* be called twice, because both
Opens would fail.
joe
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm