TheName
Local
External
Thread Local Storage Without using thread local storage
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
HANDLE mutex = CreateMutex( 0, false, "ThreadLocalMutex");
map<DWORD, void*> g_threadLocal;
void* getThreadLocal()
{
DWORD myId = ::GetCurrentThreadId();
void* pData = 0;
WaitForSingleObject( mutex, timeOutInMillis);
if (g_threadLocal.contains(myId))
pData = g_threadLocal[myId];
ReleaseMutex( mutex);
return pData;
}
void setThreadLocal(void* pData)
{
DWORD myId = ::GetCurrentThreadId();
WaitForSingleObject( mutex, timeOutInMillis);
g_threadLocal[myId] = pData;
ReleaseMutex( mutex);
}
void example()
{
void* pData;
pData = getThreadLocal();
if (!pData)
{
pData = AllocateWhatever();
setThreadLocal(pData);
}
// Use pData here
}