本人想知道 为啥多线程在对待局部类对象和局部变量的时候会有区别 本人编译器为vs2005 例一:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;CRITICAL_SECTION cs;
void PlusSection(LPVOID lp);DWORD WINAPI SectionThread1(LPVOID lp)
{
int a = 0;
while(true)
{
PlusSection(&a);
::Sleep(200);
}
return 0;
}DWORD WINAPI SectionThread2(LPVOID lp)
{
int b = 0; while( true )
{
PlusSection(&b);
::Sleep(200);
} return 0;
}void PlusSection(LPVOID lp)
{
::EnterCriticalSection(&cs);
int* np = static_cast<int*>(lp); if( np != NULL )
{
cout<<++(*np)<<endl;
cout<<"this is ++(*np)"<<endl;
cout<<--(*np)<<endl;
cout<<"this is --(*np)"<<endl;
} ::LeaveCriticalSection(&cs);
}int _tmain(int argc, _TCHAR* argv[])
{
::InitializeCriticalSection(&cs);
::CloseHandle(::CreateThread(NULL, 0, SectionThread1, NULL, 0, 0));
::CloseHandle(::CreateThread(NULL, 0, SectionThread2, NULL, 0, 0));
int i = 1; while(true)
{
cin>>i; if(!i)
{
::DeleteCriticalSection(&cs);
break;
}
} return 0;
}以上例子 两个线程可以同步执行 没有任何问题!!!!!绝对的没有问题!!!!!!!!!!那为啥下面的例子就有问题了呢 下面例子中 两个线程不能同步执行 例二:
//main.cpp#include "stdafx.h"
#include "CSection.h"
#include <iostream>
using namespace std;
DWORD WINAPI Thread1(LPVOID lp);
DWORD WINAPI Thread2(LPVOID lp);
int nStopThread = 1;int _tmain(int argc, _TCHAR* argv[])
{
::CloseHandle(::CreateThread(NULL, 0, Thread1, NULL, 0, 0));
::CloseHandle(::CreateThread(NULL, 0, Thread2, NULL, 0, 0)); while(true)
{
cin>>nStopThread;
if(!nStopThread)
{
break;
}
} return 0;
}DWORD WINAPI Thread1(LPVOID lp)
{
MYSection mySection; while(true)
{
if(!nStopThread)
{
return 0;
} mySection.PlusItself();
::Sleep(200);
}
}DWORD WINAPI Thread2(LPVOID lp)
{
MYSection mySection; while(true)
{
if(!nStopThread)
{
return 0;
} mySection.PlusItself();
::Sleep(200);
}
}//CSection.h#ifndef HEADER_SECTION
#define HEADER_SECTION
#include <windows.h>class MYSection
{
public:
MYSection();
~MYSection();
void PlusItself();
private:
int m_nCount;
};#endif //HEADER_SECTION//CSection.cpp
#include "stdafx.h"
#include "CSection.h"
#include <iostream>
using namespace std;CRITICAL_SECTION cs;MYSection::MYSection():m_nCount(0)
{
cout<<"constructor"<<endl;
::InitializeCriticalSection(&cs);
}MYSection::~MYSection()
{
cout<<"destructor"<<endl;
::DeleteCriticalSection(&cs);
}void MYSection::PlusItself()
{
::EnterCriticalSection(&cs);
cout<<++m_nCount<<endl;
cout<<"this is ++"<<endl;
cout<<--m_nCount<<endl;
cout<<"this is --"<<endl;
::LeaveCriticalSection(&cs);
}上面 俩线程有点打架 不能同步执行 这两个例子的不同就是 例一中的两个线程函数中是局部变量 例二中的两个线程函数中是局部类对象 windows对待变量和对象是不同的态度吗??顺便问一下 为啥不能把Sleep()放在LeaveCriticalSection之前呢?????
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;CRITICAL_SECTION cs;
void PlusSection(LPVOID lp);DWORD WINAPI SectionThread1(LPVOID lp)
{
int a = 0;
while(true)
{
PlusSection(&a);
::Sleep(200);
}
return 0;
}DWORD WINAPI SectionThread2(LPVOID lp)
{
int b = 0; while( true )
{
PlusSection(&b);
::Sleep(200);
} return 0;
}void PlusSection(LPVOID lp)
{
::EnterCriticalSection(&cs);
int* np = static_cast<int*>(lp); if( np != NULL )
{
cout<<++(*np)<<endl;
cout<<"this is ++(*np)"<<endl;
cout<<--(*np)<<endl;
cout<<"this is --(*np)"<<endl;
} ::LeaveCriticalSection(&cs);
}int _tmain(int argc, _TCHAR* argv[])
{
::InitializeCriticalSection(&cs);
::CloseHandle(::CreateThread(NULL, 0, SectionThread1, NULL, 0, 0));
::CloseHandle(::CreateThread(NULL, 0, SectionThread2, NULL, 0, 0));
int i = 1; while(true)
{
cin>>i; if(!i)
{
::DeleteCriticalSection(&cs);
break;
}
} return 0;
}以上例子 两个线程可以同步执行 没有任何问题!!!!!绝对的没有问题!!!!!!!!!!那为啥下面的例子就有问题了呢 下面例子中 两个线程不能同步执行 例二:
//main.cpp#include "stdafx.h"
#include "CSection.h"
#include <iostream>
using namespace std;
DWORD WINAPI Thread1(LPVOID lp);
DWORD WINAPI Thread2(LPVOID lp);
int nStopThread = 1;int _tmain(int argc, _TCHAR* argv[])
{
::CloseHandle(::CreateThread(NULL, 0, Thread1, NULL, 0, 0));
::CloseHandle(::CreateThread(NULL, 0, Thread2, NULL, 0, 0)); while(true)
{
cin>>nStopThread;
if(!nStopThread)
{
break;
}
} return 0;
}DWORD WINAPI Thread1(LPVOID lp)
{
MYSection mySection; while(true)
{
if(!nStopThread)
{
return 0;
} mySection.PlusItself();
::Sleep(200);
}
}DWORD WINAPI Thread2(LPVOID lp)
{
MYSection mySection; while(true)
{
if(!nStopThread)
{
return 0;
} mySection.PlusItself();
::Sleep(200);
}
}//CSection.h#ifndef HEADER_SECTION
#define HEADER_SECTION
#include <windows.h>class MYSection
{
public:
MYSection();
~MYSection();
void PlusItself();
private:
int m_nCount;
};#endif //HEADER_SECTION//CSection.cpp
#include "stdafx.h"
#include "CSection.h"
#include <iostream>
using namespace std;CRITICAL_SECTION cs;MYSection::MYSection():m_nCount(0)
{
cout<<"constructor"<<endl;
::InitializeCriticalSection(&cs);
}MYSection::~MYSection()
{
cout<<"destructor"<<endl;
::DeleteCriticalSection(&cs);
}void MYSection::PlusItself()
{
::EnterCriticalSection(&cs);
cout<<++m_nCount<<endl;
cout<<"this is ++"<<endl;
cout<<--m_nCount<<endl;
cout<<"this is --"<<endl;
::LeaveCriticalSection(&cs);
}上面 俩线程有点打架 不能同步执行 这两个例子的不同就是 例一中的两个线程函数中是局部变量 例二中的两个线程函数中是局部类对象 windows对待变量和对象是不同的态度吗??顺便问一下 为啥不能把Sleep()放在LeaveCriticalSection之前呢?????
解决方案 »
- 服务端多线程的问题
- 游戏子窗口是DX画的,怎么找其句柄
- 请问个关于串口的问题
- VC++小型项目外包!熟悉Unicode和Socket编程的VC高手请进!
- 有谁知道有没有vc++6.0的中文msdn library?
- 为什么会错位??
- 明天就要去上一家公司去试训了!有这方面经验的请说两句!来了就有分!UP的也有!(不够我在开)
- 各位的高手大哥们,小弟不才,毕业设计要做一个能屏幕广播的软件,辛苦了几个月,没有什么结果,实在无奈,希望大哥们指点,有源码和文档的望能发我一份
- 帮帮忙,mdi中多文档模板问题,急啊!
- 怎样获得应用程序的句柄
- SetDIBitsToDevice怎么压缩图片在picture中显示
- syntax error : missing ';' before '<'
class MYSection
{
public:
MYSection(int m);
~MYSection();
void PlusItself();
private:
int m_nCount;
};#include <iostream>
#include "CSection.h"
using namespace std;
extern CRITICAL_SECTION cs;
MYSection::MYSection(int m):m_nCount(m)
{
}
MYSection::~MYSection()
{
}
void MYSection::PlusItself()
{
::EnterCriticalSection(&cs);
cout<<++m_nCount<<"this is ++"<<endl;
cout<<--m_nCount<<"this is --"<<endl;
::LeaveCriticalSection(&cs);
}
#include <iostream>
#include "CSection.h"
using namespace std;
DWORD WINAPI Thread1(LPVOID lp);
DWORD WINAPI Thread2(LPVOID lp);
int nStopThread = 1;
CRITICAL_SECTION cs;
int main(int argc, TCHAR* argv[])
{
::InitializeCriticalSection(&cs);
::CloseHandle(::CreateThread(NULL, 0, Thread1, NULL, 0, 0));
::CloseHandle(::CreateThread(NULL, 0, Thread2, NULL, 0, 0));
while(true)
{
cin>>nStopThread;
if(!nStopThread)
{
::DeleteCriticalSection(&cs);
break;
}
}
return 0;
}DWORD WINAPI Thread1(LPVOID lp)
{
MYSection mySection(1);
while(true)
{
if(!nStopThread)
{
return 0;
}
mySection.PlusItself();
::Sleep(200);
}
}DWORD WINAPI Thread2(LPVOID lp)
{
MYSection mySection(2);
while(true)
{
if(!nStopThread)
{
return 0;
}
mySection.PlusItself();
::Sleep(200);
}
}
#include "CSection.h"
using namespace std;
DWORD WINAPI Thread1(LPVOID lp);
DWORD WINAPI Thread2(LPVOID lp);
int nStopThread = 1;
CRITICAL_SECTION cs;
int main(int argc, TCHAR* argv[])
{
::InitializeCriticalSection(&cs);
::CloseHandle(::CreateThread(NULL, 0, Thread1, NULL, 0, 0));
::CloseHandle(::CreateThread(NULL, 0, Thread2, NULL, 0, 0));
while(true)
{
cin>>nStopThread;
if(!nStopThread)
{
::DeleteCriticalSection(&cs);
break;
}
}
return 0;
}DWORD WINAPI Thread1(LPVOID lp)
{
MYSection mySection(1);
while(true)
{
if(!nStopThread)
{
return 0;
}
mySection.PlusItself();
::Sleep(200);
}
}DWORD WINAPI Thread2(LPVOID lp)
{
MYSection mySection(2);
while(true)
{
if(!nStopThread)
{
return 0;
}
mySection.PlusItself();
::Sleep(200);
}
}#include <iostream>
#include "CSection.h"
using namespace std;
extern CRITICAL_SECTION cs;
MYSection::MYSection(int m):m_nCount(m)
{
}
MYSection::~MYSection()
{
}
void MYSection::PlusItself()
{
::EnterCriticalSection(&cs);
cout<<++m_nCount<<"this is ++"<<endl;
cout<<--m_nCount<<"this is --"<<endl;
::LeaveCriticalSection(&cs);
}
#include <windows.h>
class MYSection
{
public:
MYSection(int m);
~MYSection();
void PlusItself();
private:
int m_nCount;
};
++
这样就浪费了cpu的时间了。效率变低了。