Why does the semaphore hangs if the initial count is zero?
up vote
0
down vote
favorite
This code works fine, it acquires the one semaphore entry
static void Main(string args)
{
Semaphore semaphore = new Semaphore(1, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
but this one waits on the WaitOne() method.
static void Main(string args)
{
Semaphore semaphore = new Semaphore(0, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
Am I missing something basic here? Thanks
c# concurrency
add a comment |
up vote
0
down vote
favorite
This code works fine, it acquires the one semaphore entry
static void Main(string args)
{
Semaphore semaphore = new Semaphore(1, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
but this one waits on the WaitOne() method.
static void Main(string args)
{
Semaphore semaphore = new Semaphore(0, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
Am I missing something basic here? Thanks
c# concurrency
1
Umm, yes. A Semaphore with count=0 blocks by its nature.
– Klaus Gütter
Nov 10 at 20:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This code works fine, it acquires the one semaphore entry
static void Main(string args)
{
Semaphore semaphore = new Semaphore(1, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
but this one waits on the WaitOne() method.
static void Main(string args)
{
Semaphore semaphore = new Semaphore(0, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
Am I missing something basic here? Thanks
c# concurrency
This code works fine, it acquires the one semaphore entry
static void Main(string args)
{
Semaphore semaphore = new Semaphore(1, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
but this one waits on the WaitOne() method.
static void Main(string args)
{
Semaphore semaphore = new Semaphore(0, 1, "sem1");
semaphore.WaitOne();
Console.WriteLine("Press any Key to release semaphore");
Console.ReadKey();
semaphore.Release();
}
Am I missing something basic here? Thanks
c# concurrency
c# concurrency
edited Nov 10 at 22:07
marc_s
565k12610921245
565k12610921245
asked Nov 10 at 20:43
RollRoll
3,4421051111
3,4421051111
1
Umm, yes. A Semaphore with count=0 blocks by its nature.
– Klaus Gütter
Nov 10 at 20:49
add a comment |
1
Umm, yes. A Semaphore with count=0 blocks by its nature.
– Klaus Gütter
Nov 10 at 20:49
1
1
Umm, yes. A Semaphore with count=0 blocks by its nature.
– Klaus Gütter
Nov 10 at 20:49
Umm, yes. A Semaphore with count=0 blocks by its nature.
– Klaus Gütter
Nov 10 at 20:49
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You are setting the amount of available requests to zero and then trying to acquire the semaphore. Since there are no available requests, the thread will hang on the semaphore and wait for some other thread to release it.
Think of the semaphore as an integer S
. When you WaitOne
, two things can happen
- If
S
is greater than zero, decrease it by one. - If
S
is equal to zero, suspend the thread until it's not zero.
In the first example, you initialize it to one and then acquire.
In the second, you initialize it to zero, so the thread waits. And it waits indefinitely, since there are no other threads to release the semaphore.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You are setting the amount of available requests to zero and then trying to acquire the semaphore. Since there are no available requests, the thread will hang on the semaphore and wait for some other thread to release it.
Think of the semaphore as an integer S
. When you WaitOne
, two things can happen
- If
S
is greater than zero, decrease it by one. - If
S
is equal to zero, suspend the thread until it's not zero.
In the first example, you initialize it to one and then acquire.
In the second, you initialize it to zero, so the thread waits. And it waits indefinitely, since there are no other threads to release the semaphore.
add a comment |
up vote
0
down vote
You are setting the amount of available requests to zero and then trying to acquire the semaphore. Since there are no available requests, the thread will hang on the semaphore and wait for some other thread to release it.
Think of the semaphore as an integer S
. When you WaitOne
, two things can happen
- If
S
is greater than zero, decrease it by one. - If
S
is equal to zero, suspend the thread until it's not zero.
In the first example, you initialize it to one and then acquire.
In the second, you initialize it to zero, so the thread waits. And it waits indefinitely, since there are no other threads to release the semaphore.
add a comment |
up vote
0
down vote
up vote
0
down vote
You are setting the amount of available requests to zero and then trying to acquire the semaphore. Since there are no available requests, the thread will hang on the semaphore and wait for some other thread to release it.
Think of the semaphore as an integer S
. When you WaitOne
, two things can happen
- If
S
is greater than zero, decrease it by one. - If
S
is equal to zero, suspend the thread until it's not zero.
In the first example, you initialize it to one and then acquire.
In the second, you initialize it to zero, so the thread waits. And it waits indefinitely, since there are no other threads to release the semaphore.
You are setting the amount of available requests to zero and then trying to acquire the semaphore. Since there are no available requests, the thread will hang on the semaphore and wait for some other thread to release it.
Think of the semaphore as an integer S
. When you WaitOne
, two things can happen
- If
S
is greater than zero, decrease it by one. - If
S
is equal to zero, suspend the thread until it's not zero.
In the first example, you initialize it to one and then acquire.
In the second, you initialize it to zero, so the thread waits. And it waits indefinitely, since there are no other threads to release the semaphore.
answered Nov 10 at 20:56
V0ldek
2,112624
2,112624
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243231%2fwhy-does-the-semaphore-hangs-if-the-initial-count-is-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Umm, yes. A Semaphore with count=0 blocks by its nature.
– Klaus Gütter
Nov 10 at 20:49