Discussion:
[Lazarus] Using semaphores to limit thread access
kapibara
2015-07-19 16:32:38 UTC
Permalink
|Hi guys

A number of threads need to access an object, but no more than three threads must access simultaneously.

Thats a job for a semaphore someone said.|||I understand|that under Delphi and Windows something like this could be used:

FSemaphoreHandle:= CreateSemaphore(nil, 3, 3, nil);
||WaitForSingleObject(FSemaphoreHandle, <timeout>);|

How to do that under Lazarus and Linux?

I have found TThreadManager with its SemaphoreInit and SemaphoreWait etc. But how to use it, especially how do you specify the number of threads that can access the object?

Using 32 bit Lazarus trunk and FPC trunk.
Sven Barth
2015-07-19 20:33:29 UTC
Permalink
Post by kapibara
Hi guys
A number of threads need to access an object, but no more than three
threads must access simultaneously.
Post by kapibara
Thats a job for a semaphore someone said. I understand that under Delphi
FSemaphoreHandle := CreateSemaphore(nil, 3, 3, nil);
WaitForSingleObject(FSemaphoreHandle, <timeout>);
How to do that under Lazarus and Linux?
I have found TThreadManager with its SemaphoreInit and SemaphoreWait etc.
But how to use it, especially how do you specify the number of threads that
can access the object?
Post by kapibara
Using 32 bit Lazarus trunk and FPC trunk.
FPC does not yet have a public semaphore API. The functions in
TThreadManager are only implemented on Unix systems ans only used for
specific purposes inside the RTL.

Regards,
Sven
Michael Schnell
2015-07-20 07:04:50 UTC
Permalink
Post by kapibara
How to do that under Lazarus and Linux?
Linux and Windows: TCriticalSection.

-Michael
kapibara
2015-07-20 08:42:35 UTC
Permalink
I'm afraid that if a TCriticalSection is used, all threads except one is
blocked.

I my example, many threads are requesting a response from an object, and
this object should serve more than one thread at a time. The rest should
be blocked, as with a critical section. Here a semaphore, I understand
is very useful since it is possible to specify the number of threads
that can communicate with a shared resource simultaneously.

Some hours ago I found SDL2 headers for FreePascal:

https://github.com/danpla/sdl2-fpc

You also need SDL2.DLL:

http://libsdl.org/download-2.0.php

I took SDL2.pas and SDL2.DLL and put it in the project directory, added
sdl2 to uses, and started using it:

SDL_CreateSemaphore(MaxThreads);
SDL_SemWait(Semaphore);
SDL_SemPost(Semaphore);

It works perfectly!

Docs: https://www.libsdl.org/release/SDL-1.2.15/docs/html/thread.html
request an response from

Later I noticed that FreePascal already comes with a version of SDL in
"C:\fpc\packages\sdl"
But that version seems older than SDL2.

Btw, There is DLL's for Linux, windows, MacOS X, but not MacOS.

It would of course anyway be best not to have to use a DLL. Is it
possible to translate the semaphore part of SDL to native FreePascal?
Maybe difficult?

Or perhaps it is possible to achieve the whole thing without a semaphore?
Post by Michael Schnell
Post by kapibara
How to do that under Lazarus and Linux?
Linux and Windows: TCriticalSection.
-Michael
--
_______________________________________________
Lazarus mailing list
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Michael Schnell
2015-07-20 09:52:22 UTC
Permalink
Post by kapibara
I'm afraid that if a TCriticalSection is used, all threads except one
is blocked.
No. Only those that try to acquire the CriticalSection while they are
owned by another thread.

Critical section is just a binary (non counting) Semaphore). I never had
a problem when using them,

Of course counting semaphores and priority control for the waiting
threads in some cases is needed.

-Michael

Sven Barth
2015-07-20 09:17:37 UTC
Permalink
Post by Michael Schnell
Post by kapibara
How to do that under Lazarus and Linux?
Linux and Windows: TCriticalSection.
Critical sections are not an adequate replacement for Semaphores. Alia
since both Windows and Posix support Semaphores they'll be made available
sometime in the future since newer Delphi versions also provide a
TSemaphore.

Regards,
Sven
Loading...