Re: a synchronization issue - jusy for you guys
From: Martin Harvey (Demon Account) (martin_at__nospam_pergolesi.demon.co.uk)
Date: 06/16/04
- Next message: Martin Harvey (Demon Account): "Re: a synchronization issue - jusy for you guys"
- Previous message: Martin Harvey (Demon Account): "Re: a synchronization issue - jusy for you guys"
- In reply to: Amit: "a synchronization issue - jusy for you guys"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Martin Harvey (Demon Account): "Re: a synchronization issue - jusy for you guys"
- Previous message: Martin Harvey (Demon Account): "Re: a synchronization issue - jusy for you guys"
- In reply to: Amit: "a synchronization issue - jusy for you guys"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|