Multi-threaded variable synchronization problem has always been a major problem that plagues VC programmers. Its main performance is in multi-threaded mode, and the access to shared variables between threads is no longer safe. The programmer cannot ensure that the value of the variable is accessed. Whether it is accurately credible, in this, Microsoft gives the solution to protect using critical area objects. However, the use of the critical area is too cumbersome, and it is necessary to constantly lock and unlock, it is easy to forget, causing new bugs.
Here, the East Building gives a multithreaded variable template, which can be born with vast majority variable types to achieve safety and exchange of various variables between threads. This template has been overloaded with almost all operators, and you can easily use , -, =, - =, etc., convenient to program.
#ifndef _xiaogemvartempletehasBeendefined_
#define _xiaogemvartempletehasBeendefined_
#include
Template
Class mvar
{
PUBLIC:
Mvar ()
{
m_pbegin = new char [sizeof (mvar_type)];
MEMSET (m_pbegin, 0, sizeof (mvar_type));
}
~ 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 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 Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar Typedef mvar #ndif // _ xiaogemvartempletehasbeendefined_ Everyone only needs to put this code into the mvar.h file, in the STDAFX.H of the include the header file, you can freely use the variable in the program. The above is just the type of common type of the East Building according to its own needs, and everyone can also try to derive new types. Note that when the derived variable needs to obtain the value, the .get () member function must be used, this is the only place that is not as convenient as ordinary variables. Here is a control example, you can refer to: #includen "mvar.h" Class CMYCLASS { PUBLIC: CMYCLASS (); ~ CMYCLASS (); protected: Static uint mythread (lpvoid pparam); // thread function MBOOL M_BTHREADCONTINUE; / / Thread Cycle Sign MUINT M_NTHREADCOUNT; / / Thread Counter } CMYCLASS :: CMYCLASS () { m_bthreadcontinue = true; m_nthreadcount = 0; INT I; / / Open 100 threads For (i = 0; i <100; i ) { m_nthreadcount ; / / must be accumulated before the thread is taken before the thread AFXBEGINTHREAD (Mythread, this); // Turn on thread, with this object pointer as a parameter } } CMYCLASS :: ~ CMYCLASS () { m_bthreadContinue = false; // Notify the thread to close While (m_nthreadcount! = 0) {} // Wait for all threads to exit / / Quit at this time, thread has all been safely exited !!! } Uint CMYCLASS :: Mythread (LPVOID PPARAM) { CMYCLASS * PTHIS = (CMYCLASS *) PPARAM; // Get this object pointer While (pthis-> m_bthreadcontinue) { // cyclic body Sleep (100); } PTHIS-> M_NTHREADCOUNT -; // Thread counter minus 1 Return 0; } If you have any questions, please welcome everyone, if there is bug, please ask you to make timely to the East Building.