C # monitor class Lock Object

Source: Internet
Author: User
  • Abstract: This article introduces that the C # monitor class can lock an object. A thread can operate on this object only when this lock is obtained. The object lock mechanism ensures that only one thread can access this object at a time point that may cause confusion.

     

    When multiple threads share an objectCodeFor a similar problem, the lock keyword should not be used. here we need to use a class monitor in system. Threading. We can call it a monitor, which provides a solution to share resources with threads.

    The C # monitor class can lock an object. A thread can operate on this object only when this lock is obtained. The object lock mechanism ensures that only one thread can access this object at a time point that may cause confusion.

    Monitor must be associated with a specific object, but because it is a static class, it cannot be used to define the object, and all its methods are static, objects cannot be referenced. The following code describes how to use monitor to lock objects:

       
       
    1. ......
    2. Queueoqueue=Newqueue();
    3. ......
    4. Monitor. Enter (oqueue );
    5. ......
    6. // Currently, the oqueue object can only be manipulated by the current thread to monitor. Exit (oqueue );
    7. // Release the lock

    As shown above, when a thread calls monitor. when the enter () method locks an object, this object is owned by it. Other threads want to access this object, only waiting for it to use monitor. the exit () method releases the lock. To ensure that the thread can release the lock in the end, you can write the monitor. Exit () method into the finally code block in the try-catch-Finally structure.

    For any monitor-locked object, some information related to it is stored in the memory: one is the reference of the thread currently holding the lock, and the other is a reserve queue column, the queue stores the threads that are ready to obtain the lock. The third is a waiting queue, which stores the reference of the queue that is currently waiting for this object to change its status.

    When the thread that owns the object lock is about to release the lock, it uses monitor. the pulse () method notifies the first thread in the waiting queue, so the thread is transferred to the reserve queue column. When the object lock is released, the thread in the reserve queue column can immediately obtain the object lock.

    The following is an example of how to use the lock keyword and the C # monitor class to implement thread synchronization and communication. It is also a typical producer and consumer problem.

    In this routine, the producer thread and consumer thread are alternating, and the producer writes a number, the consumer immediately reads and displays (this is described in the comment)Program).

    The system namespace used is as follows: using system; using system. Threading;

    First, define the cell class of the object to be operated. In this class, there are two methods: readfromcell () and writetocell. the consumer thread will call readfromcell () to read and display the cellcontents content. The producer process will call the writetocell () method to write data to cellcontents.

    Example:

     

    Public Class Cell
    {
    Int Cellcontents; // Content in the cell object
    Bool Readerflag = False ;
    // Status flag. It can be read if it is true. If it is false, it is being written.
    Public Int Readfromcell ()
    {
    Lock ( This )// What are the lock keywords guaranteed? Please refer to the previous introduction to lock.
    {
    If (! Readerflag) // If not
    {
    Try
    {
    // Wait for the writetocell method to call the monitor. Pulse () method.
    Monitor. Wait ( This );
    }
    Catch (Synchronizationlockexception E)
    {
    Console. writeline (E );
    }
    Catch (Threadinterruptedexception E)
    {
    Console. writeline (E );
    }
    }
    Console. writeline (" Consume: {0} " , Cellcontents );
    Readerflag = False ;
    // Resets the readerflag flag, indicating that the consumption has been completed.
    Monitor. Pulse ( This );
    // Notify writetocell () method (this method is executed in another thread, waiting)
    }
    Return Cellcontents;
    }

    Public Void Writetocell ( Int N)
    {
    Lock ( This )
    {
    If (Readerflag)
    {
    Try
    {
    Monitor. Wait ( This );
    }
    Catch (Synchronizationlockexception E)
    {
    // When the synchronous method (a method other than the enter method of the monitor class) is called in the non-Synchronous Code area
    Console. writeline (E );
    }
    Catch (Threadinterruptedexception E)
    {
    // Abort when the thread is waiting
    Console. writeline (E );
    }
    }
    Cellcontents = N;
    Console. writeline ( " Produce: {0} " , Cellcontents );
    Readerflag = True ;
    Monitor. Pulse ( This );
    // Notify the readfromcell () method that is waiting in another thread
    }
    }
    }

     

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.