template<class T>
class ThreadSafe_U
{
public:
ThreadSafe_U(){
cnt = 0;
InitializeCriticalSection(&cs);
mp = (T)NULL;
}
~ThreadSafe_U(){
Lock();
DeleteCriticalSection(&cs);
}
public:
inline void Lock(){
EnterCriticalSection(&cs);
cnt++;
//printf("/nU lock~ %d/n", cnt);
}
inline BOOL tryLock(){
if(TryEnterCriticalSection(&cs)){
cnt++;
return TRUE;
}
return FALSE;
}
inline void Unlock(){
LeaveCriticalSection(&cs);
cnt--;
if(cnt<=0)cnt=0;
}
T getVal(){
T p;
Lock(); p = mp; Unlock();
return p;
}
T setVal(T pSet){
T p;
Lock(); p = mp = pSet; Unlock();
return p;
}
private:
T mp;
CRITICAL_SECTION cs;
int cnt;
};
#include "tools.h"
static inline void Loginfo_uu(const TCHAR* tszLogfilename, const TCHAR* tszFormat, ...)
{
va_list va;
TCHAR *pTsz;
TCHAR tszInfo[1024];
TCHAR tszFilepath[MAX_PATH];
tcscpy(tszFilepath, _T("//storage card//"));
tcscat(tszFilepath, tszLogfilename);
HANDLE hFile = CreateFile(tszFilepath, GENERIC_READ|GENERIC_WRITE,// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_ALWAYS,// How to open the
0, // Port attributes
NULL); // Handle to port with attribute to copy
if(ISVALIDHANDLE(hFile))
{
SYSTEMTIME systime;
GetLocalTime(&systime);
tcsprintf(tszInfo, _T("[%d/%d/%d %d:%d:%d]:"), systime.wYear, systime.wMonth, systime.wDay,
systime.wHour, systime.wMinute, systime.wSecond);
pTsz = tszInfo+ tcslen(tszInfo);
if(pTsz)
{
va_start(va, tszFormat);
_vstprintf(pTsz, tszFormat, va);
va_end(va);
}
tcscat(tszInfo, L"/r/n");
SetFilePointer(hFile, 0, 0, FILE_END);
DWORD dwWrited = tcslen(tszInfo)*sizeof(TCHAR);
WriteFile(hFile, tszInfo, tcslen(tszInfo)*sizeof(TCHAR), &dwWrited, NULL);
SetEndOfFile(hFile);
CloseHandle(hFile);
hFile =0;
}//else
// printf("Log Error Code %d/n", GetLastError());
}