Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2006 Feb : Semaphores

www.cryer.info
Managed Newsgroup Archive

Semaphores

Subject:Semaphores
Posted by:"Dale McKay" (dale.mck..@safeworld.net.au)
Date:Wed, 15 Feb 2006 17:04:39 +1000

Hi all, just got a couple of questions about Semaphores.

Here's a brief summary of what I am trying to achieve:

I am writing a thread-safe FIFO queue.
The Queue Class has a Semaphore to notify any relevant Threads that an item
is available in the queue.

The Semaphore is created with an initial value of 0 as follows:
FNotifySemaphore := CreateSemaphore( nil, 0, MAXINT, nil );

After enque(ing) an Item I basically increment the Semaphore counter:
ReleaseSemaphore( FNotifySemaphore, 1, nil );

Before dequeue(ing) I block the Thread until an item is available:
if ( WaitForSingleObject( FNotifySemaphore, INFINITE) = WAIT_OBJECT_0 ) then
...

The Queue Class has a Clear function that simply empties the Queue.
Within the Clear function, I need to reset the Semaphore counter back to 0
(since there are now no items in the Queue).

This has raised the following questions:

1. There does not appear to be any mechanism for resetting the count value
of a Semaphore.
    Can this only be done by releasing the Semaphore handle and then
recreating the Semaphore ?
ie.
  CloseHandle( FNotifySemaphore );
  FNotifySemaphore := CreateSemaphore( nil, 0, MAXINT, nil );

2. What happens if the handle of a Semaphore is released while a Thread is
waiting on a WaitForSingleObject call for that particular Semaphore handle ?

thanks for any help
Dale McKay

Glossary

Replies:

www.cryer.info
Managed Newsgroup Archive