Windows version of the thread lock

Source: Internet
Author: User

Simply put, when a thread executes the entercritialsection, the information inside the CS is modified to indicate which thread is consuming it. At this point, no resources are "locked". No matter what resources, other threads can still be accessed (of course, the result of execution may be wrong). However, before the thread has executed leavecriticalsection, the other thread encounters the Entercritialsection statement, and the thread is in a wait state, which is equivalent to being suspended. In this case, it is the role of protecting shared resources.
It is also because critical_section is so useful that the statements that access the shared resources in each thread must be placed between Entercritialsection and LeaveCriticalSection. This is a very easy place for beginners to overlook.

 #include <windows.h>class   ctlock{   public  : Ctlock ();   ~ctlock (); Critical_section M_cs;    //   void   Lock () {Entercriticalsectio  N ( &m_cs); } inline  void   Unlock () {leavecriticalsec  tion ( &m_cs); }    }; Ctlock::ctlock () {initializecriticalsection ( &m_cs);} Ctlock::  ~ctlock () {deletecriticalsection ( & m_cs);  

Windows version of the thread lock

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.