This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
Re: semaphores
Ross wrote:
> Hi John/Rob,
>
> Bossom, John wrote:
> > sem_getvalue()?
> >
> There's no test in the current test suite for this routine (will do
> though). And the routine would seem to need to be rewritten.
>
> Regards.
> Ross
Thanks Ross. I've thought of one or two workarounds (which mostly
involve using ReleaseSemaphore(semhandle,1,previouscount) followed by a
trywait to solve the NT problem of not being able
to do a ReleaseSemaphore with a 0 releasecount.
However, a more elegant solution --- closer to the original intent --- could
be to exploit the following, taken from "Win32 System Programming
Second Edition" by Johnson M Hart, chapter 9, page 256 :
"The release count must be greater than zero, but if it would cause the
semaphore count to exceed the maximum, the call will fail, returning FALSE,
and the count will remain unchanged.
Releasing a semaphore with a large count is a method used to obtain the
current count atomically (of course, another
thread might change the value immediately)."
So instead of
if ( ReleaseSemaphore( (*sem)->sem, 0L, &value) )
{
*sval = value;
}
this might be a good alternative
if ( ReleaseSemaphore( (*sem)->sem, GREATER_THAN_MAXIMUM_SEM_VALUE,
&value) )
{
*sval = value;
}
Sorry that I've not hacked this into the source and sent you a correct
patch, but I'm still not confident with VC++ builds, and
I'm still a bit clueless as to how to set up a pthreadVCE.lib (which I need)
as opposed to a pthreadVC.lib (the standard
build for the distro's .dsp file).
OK. Thanks again for all the advice. It is really appreciated.
Rob