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 8458035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:58:10+00:00 2026-06-10T12:58:10+00:00

http://www.codeproject.com/Articles/28785/Thread-synchronization-Wait-and-Pulse-demystified Queues: The ready queue is the collection of threads that are waiting for

  • 0

http://www.codeproject.com/Articles/28785/Thread-synchronization-Wait-and-Pulse-demystified

Queues:

The ready queue is the collection of threads that are waiting for a
particular lock. The Monitor.Wait methods introduce another queue: the
waiting queue. This is required as waiting for a Pulse is distinct
from waiting to acquire a lock. Like the ready queue, the waiting
queue is FIFO.

Recommended pattern:

These queues can lead to unexpected behaviour. When a Pulse occurs,
the head of the waiting queue is released and is added to the ready
queue. However, if there are other threads in the ready queue, they
will acquire the lock before the thread that was released. This is a
problem, because the thread that acquires the lock can alter the state
that the pulsed thread relies on. The solution is to use a while
condition inside the lock statement

*Q = Queue.

By that, I understand that when I call Pulse, it does 2 things before it ends. Firstly, it removes one thread from the waiting Q to the ready Q. Secondly, it lets one thread (without knowing who is that thread) in the Ready Q acquire the lock; it doesn’t care who acquires the lock (the thread that came from the waiting Q or a thread that was in the ready Q for some reason).

If I’m right about that then why is putting a while before Monitor.Wait helping to fix the problem (problem – the pulse ends even if the thread that came from the waiting Q didn’t acquire the lock)?

A. Tell me if I’m right about the purpose of Monitor.Pulse.

B. Why do I need to put a while before Monitor.Wait

The full code of the answer below:

class Program
{
    static Queue<int> queue = new Queue<int>();
    static object someMonitor = new object();
    
    static void Main(string[] args)
    {
        Thread Thread1 = new Thread(WorkAlltheTime);
        Thread1.Name = "Thread1";
        Thread Thread2 = new Thread(WorkAlltheTime);
        Thread2.Name = "Thread2";
        Thread Thread3 = new Thread(WorkOnce);
        Thread3.Name = "Thread3";
        Thread1.Start();
        Thread2.Start();
        Thread.Sleep(1000);
        Thread3.Start();
        Console.ReadLine();
    }
    
    static void WorkAlltheTime()
    {
        Console.WriteLine("Came in to Ready Q: " + Thread.CurrentThread.Name);
        lock (someMonitor)
        {
            Console.WriteLine("Came out from Ready Q: " + Thread.CurrentThread.Name);
            // Broken!
            while (queue.Count == 0)
            {
                Console.WriteLine("Came in to Waiting Q: " + Thread.CurrentThread.Name);
                Monitor.Wait(someMonitor);
                Console.WriteLine("Came out from Waiting Q: " + Thread.CurrentThread.Name);
            }
            queue.Dequeue();
            Console.WriteLine("Thread: "+Thread.CurrentThread.Name+" Pulled Out");
        }
    }
    
    static void WorkOnce()
    {
        lock (someMonitor)
        {
            queue.Enqueue(1);
            Monitor.Pulse(someMonitor);
        }
    }   
}
  • 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-10T12:58:12+00:00Added an answer on June 10, 2026 at 12:58 pm

    Imagine you’re trying to write a producer/consumer queue – you Pulse each time you produce an item, and a consumer needs to wait until there’s an item to consume. You’d write code like this:

    Foo item;
    lock(someMonitor)
    {
        while (queue.Count == 0)
        {
            Monitor.Wait(someMonitor);
        }
        item = queue.Dequeue();
    }
    // Use the item
    

    Suppose you didn’t have the while loop, and instead wrote:

    Foo item;
    lock(someMonitor)
    {
        // Broken!
        if (queue.Count == 0)
        {
            Monitor.Wait(someMonitor);
        }
        item = queue.Dequeue();
    }
    // Use the item
    

    Now suppose you have one thread already waiting, and then another thread just before the lock statement… then a producer pulses the monitor (and adds an item to the queue, of course).

    At that point, it’s entirely feasible that the thread which hasn’t even got to the lock yet will be the first to aquire the lock… at which point by the time the “waiting” thread acquires the lock, the queue would be empty again. With just a single if statement, without looping, you’d end up dequeuing when the queue is empty, which would fail.

    With the while loop, you’ll wait again until the next item is produced, which is what you really want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using an XPTable (http://www.codeproject.com/Articles/11596/XPTable-NET-ListView-meets-Java-s-JTable) and try to add a comboBox column. The
I have done some looking and I found this: http://www.codeproject.com/Articles/14439/The-ScrollableListBox-Custom-Control-for-ASP-NET-2 but to me it
I'm using C++ and SQLite with the CppSQLite wrapper (http://www.codeproject.com/Articles/6343/CppSQLite-C-Wrapper-for-SQLite) and I keep getting
I'm trying to implement the example outlined here: http://www.codeproject.com/Articles/30994/Introduction-to-WPF-Templates The author states The ContentPresenter
I've used following code for parsing XML file in c++. http://www.codeproject.com/Articles/176236/Parsing-an-XML-file-in-a-C-C-program . Now I
I want to make an icon overlay like here: http://www.codeproject.com/Articles/7484/How-to-overlay-an-icon-over-existing-shell-objects I downloaded this source
Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
I've recently stumbled across the ability to use boomarks in Visual Studio. ( http://www.codeproject.com/Articles/42973/Using-Bookmark-in-Visual-Studio.aspx
I'm following this http://www.codeproject.com/Articles/10020/Using-managed-code-in-an-unmanaged-application The example consist of 3 binaries: C# code C++/CLI code
'm using the project http://www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers to test the PayPal's sandbox. I have a sandbox

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.