- 论坛徽章:
- 0
|
这是我从别的地方转过来的!
这是一道中级题. 高级题其实还不如这道题
1) 指出以下示意代码的错误之处
- 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) 如果MyClass改成如下定义,请写出实现上面功能的代码
- class MyClass
- {
- static const CRITICAL_SECTION m_CriticalSection;
- public:
- MyClass() { EnterCriticalSection(m_CriticalSection); }
- ~MyClass(){ ExitCriticalSection(m_CriticalSection); }
- };
复制代码 |
|