Re: a synchronization issue - jusy for you guys

From: Martin Harvey (Demon Account) (martin_at__nospam_pergolesi.demon.co.uk)
Date: 06/16/04


Date: Wed, 16 Jun 2004 19:29:02 +0100

On Sun, 6 Jun 2004 15:46:18 +0200, "Amit" <amit_t@chief.co.il> wrote:

>i have a number of worker threads, getting a file name when created and
>doing some processing with that file. some of the threads might get the same
>file name, and in that case i want the first one who got to it to "tell" all
>the other ones with that same file to wait untill its done. When it is done,
>i want the next thread to "lock" the file for itself and "release" it to
>others when it is done, and so on... the reason im using "" is because i
>want all synchronization to be external, and NOT by file locking mechanizm.

Why? The file locking mechanism is one of those things an OS provides
which you can guarantee will be atomic.

>i tried the following in my worker threads:
>
> hEvent := OpenEvent(EVENT_ALL_ACCESS, True, PChar(FileName));
> if hEvent = 0 then // if no one is processing that same file
>right now
> begin
> hEvent := CreateEvent(nil, True, False, PChar(FileName));
> end else // Someone is processing that file, wait until its done
> begin

Nice race condition. You should know that all calls to create named
objects tend to have the same semantics: you issue a call to create,
and you can then discover via the error code whether you've opened an
existing, instead of creating a new one.

CreateEvent:

Return Values
If the function succeeds, the return value is a handle to the event
object. If the named event object existed before the function call,
the function returns a handle to the existing object and GetLastError
returns ERROR_ALREADY_EXISTS.

CreateMutex:

Return Values
If the function succeeds, the return value is a handle to the newly
created mutex object.

If the function fails, the return value is NULL. To get extended error
information, call GetLastError.

If the mutex is a named mutex and the object existed before this
function call, the return value is a handle to the existing object and
GetLastError returns ERROR_ALREADY_EXISTS.

CreateSemaphore:

Return Values
If the function succeeds, the return value is a handle to the
semaphore object. If the named semaphore object existed before the
function call, the function returns a handle to the existing object
and GetLastError returns ERROR_ALREADY_EXISTS.

If the function fails, the return value is NULL. To get extended error
information, call GetLastError.

CreateFile:

Return Values
If the function succeeds, the return value is an open handle to the
specified file. If the specified file exists before the function call
and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to
GetLastError returns ERROR_ALREADY_EXISTS (even though the function
has succeeded). If the file does not exist before the call,
GetLastError returns zero.

If the function fails, the return value is INVALID_HANDLE_VALUE. To
get extended error information, call GetLastError.



Relevant Pages

  • Re: CreateDIBSection question
    ... >>>` If the function succeeds, the return value is a handle to the newly ... >I have any error message or returned by GetLastError and i think the problem ... But if the system has multiple monitors, ... Sev ...
    (microsoft.public.win32.programmer.gdi)
  • Re: SetMenu & GetLastError
    ... > Can you tell me perhaps if it is for sure, that the value of GetLastError ... > statement in the MSDN, so I am a bit sceptical about this. ... If the function succeeds, ... as it doesn't prevent SetMenu from succeeding. ...
    (microsoft.public.win32.programmer.ui)
  • How to call CreateEvent() in Windows CE 3.0 ?
    ... GetLastError indicates an invalid handle. ... So I backtrack and call GetLastError() after the call to CreateEvent(). ... WaitForMultipleObjects() and GetLastErrorcalled after CreateEvent ...
    (microsoft.public.windowsce.embedded)
  • Re: openevent win ce 5.0, returns error code 183
    ... I'm quite sure that CreateEvent and OpenEvent end up calling the same ... function in the kernel so I imagine that ou get ERROR_ALREADY_EXIST as if ... // print GetLastError() ...
    (microsoft.public.windowsce.app.development)
  • Re: How to call CreateEvent() in Windows CE 3.0 ?
    ... When this handle is passed to WaitForMultipleObjects() ... GetLastError indicates an invalid handle. ... > So I backtrack and call GetLastErrorafter the call to CreateEvent(). ... Prev by Date: ...
    (microsoft.public.windowsce.embedded)