多线程的变量同步问题,一直是困扰广大VC程序员的一大难题,其主要表现是在多线程模式下,线程间共享变量的存取不再安全,程序员无法确保变量的值在存取时是否准确可信,对此,微软给出的解决方案是使用临界区对象加以保护。但是,临界区对象使用太过于繁琐,需要不断地加锁和解锁,很容易搞忘,引起新的Bug。
这里,东楼给出多线程安全的变量模板,由此模板,可派生绝大多数变量类型,实现线程间各种变量的安全互访。本模板已经重载了几乎全部运算符,大家可以很方便地使用++、――、+=、-=等操作,方便编程使用。
#ifndef _XiaoGeMVarTempleteHasBeenDefined_
#define _XiaoGeMVarTempleteHasBeenDefined_
#include <Afxmt.h>
template <class MVAR_TYPE>
class MVAR
{
public:
MVAR()
{
m_pBegin=new char[sizeof(MVAR_TYPE)];
memset(m_pBegin,0,sizeof(MVAR_TYPE));
}
~MVAR(){delete[] m_pBegin;}
int GetLength(){return sizeof(MVAR_TYPE);}
MVAR_TYPE Set(MVAR_TYPE& value)
{
m_csLockHandle.Lock();
memcpy(m_pBegin,(char*)&value,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return value;
}
MVAR_TYPE Get()
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
myValue=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator~()
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue=~myValue;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator++(int)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue++;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator++()
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue++;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator--(int)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue--;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator--()
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue--;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator+(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue+=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator-(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue-=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator*(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue*=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator/(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue/=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator&(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue&=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator|(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue|=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator^(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue^=value;
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator+=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue+=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator-=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue-=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator*=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue*=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator/=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue/=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator&=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue&=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator|=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue|=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
MVAR_TYPE operator^=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
myValue^=value;
memcpy(m_pBegin,(char*)&myValue,sizeof(MVAR_TYPE));
m_csLockHandle.Unlock();
return myValue;
}
BOOL operator>(MVAR_TYPE value)
{
MVAR_TYPE myValue;
BOOL bRet;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
bRet=(myValue>value);
m_csLockHandle.Unlock();
return bRet;
}
BOOL operator<(MVAR_TYPE value)
{
MVAR_TYPE myValue;
BOOL bRet;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
bRet=(myValue<value);
m_csLockHandle.Unlock();
return bRet;
}
BOOL operator>=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
BOOL bRet;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
bRet=(myValue>=value);
m_csLockHandle.Unlock();
return bRet;
}
BOOL operator<=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
BOOL bRet;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
bRet=(myValue<=value);
m_csLockHandle.Unlock();
return bRet;
}
BOOL operator==(MVAR_TYPE value)
{
MVAR_TYPE myValue;
BOOL bRet;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
bRet=(myValue==value);
m_csLockHandle.Unlock();
return bRet;
}
BOOL operator!=(MVAR_TYPE value)
{
MVAR_TYPE myValue;
BOOL bRet;
m_csLockHandle.Lock();
memcpy((char*)&myValue,m_pBegin,sizeof(MVAR_TYPE));
bRet=(myValue!=value);
m_csLockHandle.Unlock();
return bRet;
}
private:
char* m_pBegin;
CCriticalSection m_csLockHandle;
};
typedef MVAR<char> MCHAR;
typedef MVAR<unsigned char> MUCHAR;
typedef MVAR<short> MSHORT;
typedef MVAR<unsigned short> MUSHORT;
typedef MVAR<int> MINT;
typedef MVAR<unsigned int> MUINT;
typedef MVAR<long> MLONG;
typedef MVAR<unsigned long> MULONG;
typedef MVAR<float> MFLOAT;
typedef MVAR<double> MDOUBLE;
typedef MVAR<BOOL> MBOOL;
typedef MVAR<LPVOID> MLPVOID;
typedef MVAR<BYTE> MBYTE;
#endif //_XiaoGeMVarTempleteHasBeenDefined_
大家只需要把这段代码放入Mvar.h文件,在stdafx.h中include这个头文件,即可在程序中自由使用变量。以上仅仅是东楼根据自己的需要,派生的几个常用类型,大家也可以尝试派生新的类型使用。
注意,当派生的变量需要取得其中的值时,必须使用.Get()成员函数,这是唯一一点不如普通变量方便的地方。
下面给出一个控制例子,大家可以参考:
#includen “mvar.h”
class CMyClass
{
public:
CMyClass();
~ CMyClass();
protected:
static UINT MyThread(LPVOID pParam); //线程函数
MBOOL m_bThreadContinue; //线程循环标志
MUINT m_nThreadCount; //线程计数器
};
CMyClass::CMyClass()
{
m_bThreadContinue=TRUE;
m_nThreadCount=0;
int i;
//开启100个线程
for(i=0;i<100;i++)
{
m_nThreadCount++; //一定要在开启线程前累加线程计数器
AfxBeginThread(MyThread,this); //开启线程,以本对象指针为参数
}
}
CMyClass::~CMyClass()
{
m_bThreadContinue=FALSE; //通知线程关闭
while(m_nThreadCount!=0) {} //等待所有线程退出
//此时退出,线程已经全部安全退出!!!
}
UINT CMyClass::MyThread(LPVOID pParam)
{
CMyClass* pThis=(CMyClass*)pParam; //得到本对象指针
while(pThis->m_bThreadContinue)
{
//循环体
Sleep(100);
}
pThis->m_nThreadCount --; //线程计数器减1
return 0;
}
如果有问题,欢迎大家提问,如果有Bug,也请大家及时向东楼提出。
本文地址:http://com.8s8s.com/it/it2502.htm