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










share|improve this question




















  • 1




    Umm, yes. A Semaphore with count=0 blocks by its nature.
    – Klaus Gütter
    Nov 10 at 20:49















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










share|improve this question




















  • 1




    Umm, yes. A Semaphore with count=0 blocks by its nature.
    – Klaus Gütter
    Nov 10 at 20:49













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 20:56









        V0ldek

        2,112624




        2,112624






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues