ArticleDirectory
- Monitor description
- Use pulse and wait
- Do not use pulse and wait
- Conclusion
Monitor description
Enter, tryenter
Obtain the object lock. This operation also marks the beginning of the critical section. No other thread can enter the critical section unless it uses other locked objects to execute commands in the critical section.
Wait
Release the lock on the object to allow other threads to lock and access the object. When other threads access an object, the calling thread will wait. The pulse signal is used to notify the waiting thread of changes to the object state.
Pulse (signal), pulseall
Sends signals to one or more waiting threads. This signal indicates that the status of the waiting thread lock object has changed, and the lock owner is ready to release the lock. Wait for the thread to be placed in the ready queue of the object so that it can finally receive the object lock. Once the thread has a lock, it can check the new State of the object to see if it has reached the desired state.
Exit
Release the lock on the object. This operation also marks the end of the critical section protected by the locked object.
Use pulse and wait
Public void docriticalsection ()
{
Monitor. Enter (this. lockme );
Console. writeline (string. Format ("docriticalsection lock held by threadid: {0}", thread. currentthread. managedthreadid ));
For (INT I = 0; I <5; I ++)
{
Monitor. Pulse (this. lockme );
Console. writeline (string. Format ("thread ID: {0} pulse", thread. currentthread. managedthreadid ));
Console. writeline (string. Format ("monitorwaitandpulse1 result: {0}", result ++ ));
Thread. Sleep (1000 );
Monitor. Wait (this. lockme );
Console. writeline (string. Format ("thread ID: {0} Wait", thread. currentthread. managedthreadid ));
}
Console. writeline (string. Format ("docriticalsection lock realestate by threadid: {0}", thread. currentthread. managedthreadid ));
Monitor. Exit (this. lockme );
}
Do not use pulse and wait
Public void docriticalsection ()
{
Monitor. Enter (this. lockme );
Console. writeline (string. Format ("docriticalsection lock held by threadid: {0}", thread. currentthread. managedthreadid ));
For (INT I = 0; I <5; I ++)
{
// Monitor. Pulse (this. lockme );
Console. writeline (string. Format ("thread ID: {0} pulse", thread. currentthread. managedthreadid ));
Console. writeline (string. Format ("monitorwaitandpulse1 result: {0}", result ++ ));
Thread. Sleep (1000 );
// Monitor. Wait (this. lockme );
Console. writeline (string. Format ("thread ID: {0} Wait", thread. currentthread. managedthreadid ));
}
Console. writeline (string. Format ("docriticalsection lock realestate by threadid: {0}", thread. currentthread. managedthreadid ));
Monitor. Exit (this. lockme );
}
Conclusion
use monitor. wait and monitor. the pulse can be executed on threads alternately. If you only use enter and exit, when Code enters monitor. after entering, the current thread does not monitor. exit. Other threads can only wait. monitor. after pulse, the lock can be obtained and executed in the next line. Monitor. wait and wait for other threads to release the lock.