This is a medium-sized question. The high-level question is not as good as this question 1) indicating the following error critical_section g_criticalsection = null; char * g_string = null; class myclass {public: int initInstance (); int threada (); }; int MyClass :: Init () {InitCriticalSection (& g_CriticalSection); CreateThread (threadA, ...);} int MyClass :: threadA () {EnterCriticalSection (g_CriticalSection) if (! g_string = NULL) {delete g_string;} g_string = new char [1024]; ... delete g_string; g_string = NULL; ExitCriticalSection (g_CriticalSection) return 0;} 2) If MyClass into the following definitions, write code to achieve the above function class MyClass {static const CRITICAL_SECTION m_CriticalSection Public: myclass () {EntercriticalSection;} ~ myclass () {exitcriticalsection;}}; (1) I just talk about what I think about Int myclass :: init () {initcriptagesection (& g_criticalsection);
Createthread (threada, ...);} From the point of view, after a MyClass, you should call init () to initialize, the problem is in the initialization. It should not be not necessarily, should not put the initialization of the critical region in the constructor of the class, so that every initialization of an object is initialized, the variable representative of critical resources is initialized once, if this has been an object before The critical area is inevitable. To avoid this, the most basic method is to put the initialization of the critical regions in a global location, after all, the critical region variable is also a global variable. (2) If 2) is required to achieve it does not know that okay: class MyClass {static const CRITICAL_SECTION m_CriticalSection; public: MyClass () {EnterCriticalSection (m_CriticalSection);} ~ MyClass () {ExitCriticalSection (m_CriticalSection);} int threadA (); Int}; int myclass :: init () {createthread (threada, ...);} int myclass :: threada () {i (g_string! = Null) {delete g_string;} g_string = New char [1024]; ... delete g_string; g_string = null; return 0;} int initcripticalsection (critical_section * criticalsection) {...........}; start, first call initcritical thection & Myclass.m_criticalsection; then use the MyClass object feel like this?