[fpc-pascal] Re: Semaphore problems

Graeme Geldenhuys graemeg.lists at gmail.com
Mon Jul 24 16:27:03 CEST 2006

Hi Vinzent,

You got me one the right track, I think... :-)

I thought the counter in Semaphores get incremented, but as you stated
(and reading some Man Pages), they get decremented.

I then change my code from:

> >   if sem_init(FSemaphore, 0, 1) <> 0 then
> >     raise Exception.Create('Failed to create the semaphore');

to ...

> >   if sem_init(FSemaphore, 0, FiMaxPoolSize) <> 0 then
> >     raise Exception.Create('Failed to create the semaphore');

Which makes more sense now...  FiMaxPoolSize is the maximum number of
requests being allow.  Having it set to 1, I was only allowed 1, so
after the first Lock, the others ended in a deadlock type situation.
Hence the second iteration freezing the app.

> What I don't understand is why you don't use the "SyncObjs" unit where
> almost all the stuff someone ever needs is nicely laid out in a
> portable and object-oriented way.

I didn't write the original Windows code, just tried to port it as is
to FPC and get the Unit Tests to pass.  I will speak to the original
author and see what he thinks of you idea. As long as the code can
still compile under Delphi and FPC, I am sure everything should be

> > >   if sem_wait(FSemaphore) <> 0 then
> > >     raise
> > > EtiOPFInternalException.Create(cErrorTimedOutWaitingForSemaphore);
> BTW, sem_wait() never returns anything else than zero, so checking is
> sort of useless here. There's sem_trywait() for that, if you really
> need to go so low-level.

I changed it to sem_trywait() which pretty made all my tiPool unit
tests pass.  For some strange reason I still get a lock-up, but much
less now.  Say once every 5 iterations of the complete test suite
where-as before it was every time on a specific test case.  I will now
try and implement the timeout work-around as suggested in the IBM
article below.

I found the IBM article very handy to explain some information.

Thanks for you time!


There's no place like

More information about the fpc-pascal mailing list