Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-07

Using semaphores to limit thread access

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

kapibara

|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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150719/966fd8f3/attachment.html>

Reply by email

Sven Barth
to kapibara
"kapibara" wrote
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:

Show quoted text 7 lines
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.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150719/1ee455cf/attachment.html>

Reply by email

kapibara
to Michael Schnell

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?

Michael Schnell wrote 13 lines
On 07/19/2015 06:32 PM, kapibara wrote: > How to do that under Lazarus and Linux? > Linux and Windows: TCriticalSection. -Michael -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150720/4b173b6a/attachment.html>

Reply by email

Sven Barth
to Michael Schnell: Linux and Windows: TCriticalSection. -Michael -------------- next part -------------- An…
"Michael Schnell" wrote 6 lines
On 07/19/2015 06:32 PM, kapibara wrote: > > 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150720/52cd84ea/attachment.html>

Reply by email

Michael Schnell
to kapibara: I'm afraid that if a TCriticalSection is used, all threads except one is blocked. I my…
kapibara wrote
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150720/d050dbdb/attachment.html>

Reply by email