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

  • Home
  • SEARCH
  • 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 6854595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:39:11+00:00 2026-05-27T01:39:11+00:00

I was looking at Example 2 on this msdn page about Thread synchronization with

  • 0

I was looking at Example 2 on this msdn page about Thread synchronization with Monitor.Pulse().

A Cell object is created and passed to both the producer and the consumer objects.

Cell cell = new Cell( );
CellProd prod = new CellProd(cell, 20);
CellCons cons = new CellCons(cell, 20);

A thread is created for each of those two

Thread producer = new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer = new Thread(new ThreadStart(cons.ThreadRun));

The ThreadRun in each case being a loop that calls Cell.ReadFromCell() or Cell.WriteToCell() depending on consumer/producer. For example, the producer does this

public void ThreadRun( )
{
  for(int looper=1; looper<=quantity; looper++)
     cell.WriteToCell(looper);  // "producing"
}

The bit I don’t understand is that in each of these methods they start with a

lock(this)

And since it was the same Cell object (ie, the ‘this’ in the above lock statement) passed to both, I would have thought that only one thread could be in this section of code at a time. Yet from Monitor.Pulse() and Monitor.Wait() code that follows it looks like both threads are in those sections at the same time. If one had the lock and hit a Monitor.Wait() then the other thread could never Pulse because it is blocked waiting for the lock.

I’m guessing there is a simple solution and I have misunderstood something, from my tests with running the code it looks like both threads are in their critical sections at the same time, so the lock(this) isn’t doing what I was under the impression it should do.

  • 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-05-27T01:39:12+00:00Added an answer on May 27, 2026 at 1:39 am

    The thing you’re missing is that Monitor.Wait(this) will release the lock on this until it’s woken up. So yes, your consumer thread looks like it’s in the lock and therefore owns it, but really it’s temporarily released it.

    From the docs:

    When a thread calls Wait, it releases the lock on the object and enters the object’s waiting queue. The next thread in the object’s ready queue (if there is one) acquires the lock and has exclusive use of the object. All threads that call Wait remain in the waiting queue until they receive a signal from Pulse or PulseAll, sent by the owner of the lock. If Pulse is sent, only the thread at the head of the waiting queue is affected. If PulseAll is sent, all threads that are waiting for the object are affected. When the signal is received, one or more threads leave the waiting queue and enter the ready queue. A thread in the ready queue is permitted to reacquire the lock.

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

Sidebar

Related Questions

Looking at the example code on MSDN : This ' Declares a type. Dim
Looking at this example of named pipes using Overlapped I/O from MSDN, I am
I'm looking for an example scenario where this function could be used. MSDN documentation
looking at this example of the jquery ui slider http://jqueryui.com/demos/slider/#steps i want to be
I am looking at this example: List<Product> products = Product. GetSampleProducts() ; products.Sort( (first,
thank you for looking i got this example from my book but i cant
Looking for a proven to work algorithm for production. Did see this example but
Looking at this jQuery example, how can I modify the code so that it
Using this MSDN Article as an example for pausing and resuming, one would assume
I took the code from this example. http://msdn.microsoft.com/en-us/library/2tw134k3.aspx What I am wondering (and I've

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.