Hi all,

@Serguei Thanks for your reply. What you write is probably right, but let me explain the problem.

In an application you have N number of threads and they all want to access a service. Now, for example, only five threads are allowed to have access at the same time. I believe this is a job for a counting semaphore. Delphi also has a semaphore. The code I wrote, as basic as it is, limits the simultaneous thread accesses to the service. So the semaphore is used here for synchronization of threads within the same application, and that is one of the intended uses of semaphores. A critical section blocks all threads except one, so I'm under the impression that it can't be used for this task.

Reason I wrote is because I have a feeling the code I wrote could be improved, or implemented differently altogether. A few weeks ago I never even heard about "counting semaphores". There seem to be not so much time spent on this subject in education, and many different opinions about what semaphores are and what they are used for can be found.


On 2015-07-29 12:35, Serguei TARASSOV wrote:
Hello,

On 29/07/2015 12:00, fpc-pascal-requ...@lists.freepascal.org wrote:
A counting, or general, semaphore limits simultaneous access to a
resource according to the number of permits you specify. On the other
hand, a binary semaphore like a critical section limits the access to
"one at a time".
Seems, you confuse a little semaphores and critical sections.
In general, semaphores are inter-process but critical sections are not.
I mean that semaphores allow synchronize processes whereas critical sections are designed to synchronize the threads within the same process. Some frameworks (i.e. .NET) provide classes like "threading semaphore" but it is very particular case. It is possible to use real semaphores to synchronize the threads too (with some overhead).

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to