Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:33:00+00:00 2026-06-09T19:33:00+00:00

Finally here is the edit: I am synchronizing two different processes with a semaphore:

  • 0

Finally here is the edit:

I am synchronizing two different processes with a semaphore:

var semaphore = new Semaphore(0, 1, "semName");

so I have process A and process B they happen to be different executables.

Process A

    static void Main(string[] args)
    {

        Console.Write("Process A");
        Task.Factory.StartNew(() =>
        {
            var semaphoreName = "sem";
            var semaphore = Semaphore.OpenExisting(semaphoreName);

            Thread.Sleep(100);

            semaphore.Release();

            semaphore.WaitOne();

            semaphore.Release();

            Console.Write("Process A Completed!");
        });
        Console.Read();

    }

Process B (Different console app)

    static void Main(string[] args)
    {
        Console.Write("Process B");
        Task.Factory.StartNew(() =>
        {
            var semaphoreName = "sem";
            var semaphore = new Semaphore(0, 1, semaphoreName);

            semaphore.WaitOne();

            Thread.Sleep(1000);

            semaphore.Release();

            semaphore.WaitOne();

            Console.Write("Process B Completed!");
        });

        Console.Read();


    }

If I debug the processes or place a Thread.Sleep I am able to reach the last line Console.Write("Process A Completed!"); How can I solve this problem without having to place Thread.Sleep(100);?




Edit

There is no race condition!! Maybe I am wrong correct me if I am. Anyways here is the reason why I think there is no race condition:

Process A

    static void Main(string[] args)
    {
        Console.Write("Process A");
        var semaphoreName = "sem";

        Task.Factory.StartNew(() =>
        {                                
            var semaphore = Semaphore.OpenExisting(semaphoreName);

            semaphore.Release();

            semaphore.WaitOne();

            // this line should never be reached but it is!!!

            Console.Write("Process A Completed!");
        });            

        Console.Read();

    }

Processes B

    static void Main(string[] args)
    {
        Console.Write("Process B");
        var semaphoreName = "sem";

        Task.Factory.StartNew(() =>
        {                

            var semaphore = new Semaphore(0, 1, semaphoreName);

            semaphore.WaitOne();

            // important to have these lines
            int a = 0;
            for (var i = 0; i < 1000000000; i++)
                a = i;

            Thread.Sleep(10000); // there should not be a race condition any more!!!!

            Console.Write("Process B Completed!");
        });

        Console.Read();
    }

note that on process A we should never reach the line: Console.Write("Process A Completed!"); But we do…

Process B sleeps for 10000 seconds so there is no reason why there should be a race condition. Moreover if I remove the loop for (var i = 0; i < 1000000000; i++) I don’t get that behavior.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:33:02+00:00Added an answer on June 9, 2026 at 7:33 pm

    I haven’t been able to find any documentation that states that threads waiting on a semaphore will necessarily be released in the same order that they begin waiting on it. Is it not therefore possible that the sem.WaitOne() in process B is being released by the sem.Release() immediately above it, without giving process A a chance to enter?

    The addition of the Thread.Sleep() prior to waiting on the semaphore in process B presumably gives process A a chance to enter the semaphore instead.

    You’ve not made it entirely clear why you’re using a semaphore in this way, or what the sem.WaitOne() in process B is waiting for (since process A never releases the semaphore in your example code), so it’s difficult to suggest an improvement that fix the problem.

    If however you’re intending to have process A wait for process B to start before beginning its work, then for process B to wait for process A to complete the work before continuing, it seems that two ManualResetEvents could achieve this more effectively (specifically a “Process B ready” event waited on by process A, and a “Process A finished” event waited on by process B).

    Possible solution:

    Process A:

        static void Main(string[] args)
        {
            Console.Write("Process A");
            Task.Factory.StartNew(() =>
            {
                var readyEvent = new EventWaitHandle(false, EventResetMode.ManualReset, "Process B Ready");
                var doneEvent = new EventWaitHandle(false, EventResetMode.ManualReset, "Process A Finished");
    
                // Wait for process B to be ready...
                readyEvent.WaitOne();
    
                // Do some work...
    
                Console.Write("Process A Completed!");
    
                // Signal that the process is complete
                doneEvent.Set();
            });
            Console.Read();
        }
    

    Process B:

        static void Main(string[] args)
        {
            Console.Write("Process B");
            Task.Factory.StartNew(() =>
            {
                var readyEvent = new EventWaitHandle(false, EventResetMode.ManualReset, "Process B Ready");
                var doneEvent = new EventWaitHandle(false, EventResetMode.ManualReset, "Process A Finished");
    
                // Signal that process B is ready
                readyEvent.Set();
    
                // Wait for process A to complete...
                doneEvent.WaitOne();
    
                Console.Write("Process B Completed!");
            });
    
            Console.Read();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hibernate 3.5-Final is finally here and it offers the much anticipated JPA2 support, amongst
I finally have my C++ Builder 2010 installation the way I want it, with
So I've finally located a problem I have with growing memory consumption. It's the
I'm pretty much new at WCF and have a performance question. I have the
EDIT: I finally found a real simple solution to this problem, using the CAGradientLayer
here what i want to do : i have a string containing HTML tags
Finally got my cascading dropdownlist working only to find it's not repopulating in edit
Well Finally I upgrade xcode to version 4.0 as suggested by Jasarien here I
MASSIVE EDIT BASED ON THE ANSWERS BELOW original post here http://webmasters.stackexchange.com before miration. I
A simple question here, I have several classes in my code, but only one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.