[Lazarus] Using semaphores to limit thread access

kapibara kapibara.pas at aol.com
Mon Jul 20 10:42:35 CEST 2015


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?



On 2015-07-20 09:04, Michael Schnell wrote:
> 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
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

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


More information about the Lazarus mailing list