Questions tagged [semaphore]

A semaphore is a synchronization primitive that tracks how many of a limited number of resources are available.

A semaphore keeps an internal count and provides two operations: one to increment the counter and another to decrement it.

The decrement operation, known as p, acquire, or wait, puts one resource in use by the current thread, decrementing the counter. If there are no resources available, i.e., the counter is zero, the calling thread blocks until a resource becomes available.

The increment operation, known as v, release, or signal, gives one resource back; i.e. makes it available again for use by other threads.

Wikipedia entry : http://en.wikipedia.org/wiki/Semaphore_%28programming%29

Language Specific Semaphore Implementations

See also:

3319 questions
983
votes
36 answers

Difference between binary semaphore and mutex

Is there any difference between a binary semaphore and mutex or are they essentially the same?
Nitin
  • 15,151
  • 8
  • 23
  • 14
640
votes
10 answers

What is the difference between lock, mutex and semaphore?

I've heard these words related to concurrent programming, but what's the difference between lock, mutex and semaphore?
victor
  • 6,411
  • 3
  • 16
  • 5
425
votes
15 answers

What is a semaphore?

A semaphore is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a semaphore and how do you use it?
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24
271
votes
7 answers

Semaphore vs. Monitors - what's the difference?

What are the major differences between a Monitor and a Semaphore?
user919860
  • 3,083
  • 4
  • 19
  • 16
157
votes
8 answers

Conditional Variable vs Semaphore

When to use a semaphore and when to use a conditional variable?
149
votes
3 answers

Need to understand the usage of SemaphoreSlim

Here is the code I have but I don't understand what SemaphoreSlim is doing. async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); List trackedTasks = new List(); while (DoMore()) { await…
Mou
  • 15,673
  • 43
  • 156
  • 275
148
votes
13 answers

When should we use mutex and when should we use semaphore

When should we use mutex and when should we use semaphore ?
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
140
votes
10 answers

Semaphore - What is the use of initial count?

http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim.aspx To create a semaphore, I need to provide an initial count and maximum count. MSDN states that an initial count is - The initial number of requests for the semaphore that…
Sandbox
  • 7,910
  • 11
  • 53
  • 67
140
votes
5 answers

How do I choose between Semaphore and SemaphoreSlim?

Their public interfaces appear similar. The documentation states that the SemaphoreSlim is a lightweight alternative and doesn't use Windows Kernel semaphores. This resource states that the SemaphoreSlim is much faster. In what situations does…
Michael Hedgpeth
  • 7,732
  • 10
  • 47
  • 66
132
votes
8 answers

Is there a Mutex in Java?

Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e)…
Noam Nevo
  • 3,021
  • 10
  • 35
  • 49
118
votes
10 answers

What is mutex and semaphore in Java ? What is the main difference?

What is mutex and semaphore in Java ? What is the main difference ?
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
112
votes
7 answers

CountDownLatch vs. Semaphore

Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore? As far as I can tell the following fragments are almost equivalent: 1. Semaphore final Semaphore sem = new Semaphore(0); for (int i = 0;…
finnw
  • 47,861
  • 24
  • 143
  • 221
71
votes
0 answers

How to prevent a script from running simultaneously?

I want to prevent my script running more than once at a time. My current approach is create a semaphore file containing the pid of the running process read the file, if my process-id is not in it exit (you never know...) at the end of the…
Oli
  • 1,762
  • 3
  • 19
  • 22
67
votes
3 answers

Throttling asynchronous tasks

I would like to run a bunch of async tasks, with a limit on how many tasks may be pending completion at any given time. Say you have 1000 URLs, and you only want to have 50 requests open at a time; but as soon as one request completes, you open up a…
Josh Wyant
  • 1,177
  • 1
  • 8
  • 13
65
votes
3 answers

sem_init on OS X

I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my Ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a…
Nippysaurus
  • 20,110
  • 21
  • 77
  • 129
1
2 3
99 100