Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. lock and entrancy

lock and entrancy

Scheduled Pinned Locked Moved C#
csharphelptutorialquestionworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Reno Tiko
    wrote on last edited by
    #1

    I ran the producer/consumer threading lock example, and set the breakpoints right after the cell locked. From my understanding, anything within a lock block should by synchronized, and only one thread can enter into a locked block at a time. However, when the program is run, the breakpoints are hit on both of the producer and consumer threads. How can this be? The code used: // MonitorSample.cs // This example shows use of the following methods of the C# lock keyword // and the Monitor class // in threads: // Monitor.Pulse(Object) // Monitor.Wait(Object) using System; using System.Threading; public class MonitorSample { public static void Main(String[] args) { int result = 0; // Result initialized to say there is no error Cell cell = new Cell( ); CellProd prod = new CellProd(cell, 20); // Use cell for storage, // produce 20 items CellCons cons = new CellCons(cell, 20); // Use cell for storage, // consume 20 items Thread producer = new Thread(new ThreadStart(prod.ThreadRun)); Thread consumer = new Thread(new ThreadStart(cons.ThreadRun)); // Threads producer and consumer have been created, // but not started at this point. try { producer.Start( ); consumer.Start( ); producer.Join( ); // Join both threads with no timeout // Run both until done. consumer.Join( ); // threads producer and consumer have finished at this point. } catch (ThreadStateException e) { Console.WriteLine(e); // Display text of exception result = 1; // Result says there was an error } catch (ThreadInterruptedException e) { Console.WriteLine(e); // This exception means that the thread // was interrupted during a Wait result = 1; // Result says there was an error } // Even though Main returns void, this provides a return code to // the parent process. Environment.ExitCode = result; Console.WriteLine("Finished"); Console.ReadLine(); } } public class CellProd { Cell cell; // Field to hold cell object to be used int quantity = 1; // Field for how many items to produce in cell public CellProd(Cell box, int request) { cell = box; // Pass in what cell object to be used quantity = request; // Pass in how many items to produce in cell } public void ThreadRun( ) { for(int looper=1; looper<=quantity; looper++) cell.WriteToCell(looper); // "producing" } } public class CellCons { Cell cell;

    E 1 Reply Last reply
    0
    • R Reno Tiko

      I ran the producer/consumer threading lock example, and set the breakpoints right after the cell locked. From my understanding, anything within a lock block should by synchronized, and only one thread can enter into a locked block at a time. However, when the program is run, the breakpoints are hit on both of the producer and consumer threads. How can this be? The code used: // MonitorSample.cs // This example shows use of the following methods of the C# lock keyword // and the Monitor class // in threads: // Monitor.Pulse(Object) // Monitor.Wait(Object) using System; using System.Threading; public class MonitorSample { public static void Main(String[] args) { int result = 0; // Result initialized to say there is no error Cell cell = new Cell( ); CellProd prod = new CellProd(cell, 20); // Use cell for storage, // produce 20 items CellCons cons = new CellCons(cell, 20); // Use cell for storage, // consume 20 items Thread producer = new Thread(new ThreadStart(prod.ThreadRun)); Thread consumer = new Thread(new ThreadStart(cons.ThreadRun)); // Threads producer and consumer have been created, // but not started at this point. try { producer.Start( ); consumer.Start( ); producer.Join( ); // Join both threads with no timeout // Run both until done. consumer.Join( ); // threads producer and consumer have finished at this point. } catch (ThreadStateException e) { Console.WriteLine(e); // Display text of exception result = 1; // Result says there was an error } catch (ThreadInterruptedException e) { Console.WriteLine(e); // This exception means that the thread // was interrupted during a Wait result = 1; // Result says there was an error } // Even though Main returns void, this provides a return code to // the parent process. Environment.ExitCode = result; Console.WriteLine("Finished"); Console.ReadLine(); } } public class CellProd { Cell cell; // Field to hold cell object to be used int quantity = 1; // Field for how many items to produce in cell public CellProd(Cell box, int request) { cell = box; // Pass in what cell object to be used quantity = request; // Pass in how many items to produce in cell } public void ThreadRun( ) { for(int looper=1; looper<=quantity; looper++) cell.WriteToCell(looper); // "producing" } } public class CellCons { Cell cell;

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      When you call Monitor.Wait() from within the lock, the lock is released and that thread waits for another thread to call Pulse. When that happens, the lock will be re-acquired and the process will continue.

      R 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        When you call Monitor.Wait() from within the lock, the lock is released and that thread waits for another thread to call Pulse. When that happens, the lock will be re-acquired and the process will continue.

        R Offline
        R Offline
        Reno Tiko
        wrote on last edited by
        #3

        Ah, I see now. I knew I was missing something, but couldn't quite put my finger on it. Thanks for the tip! :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups