




template <class T>
struct SharedData
{
T m_pData[DATACENTER_CACHE];
T m_kCloneData;
int m_iIndex;
SharedData()
{
ZeroMemory( m_pData, DATACENTER_CACHE * sizeof(T) );
m_iIndex = 0;
}
void Write( T& rData )
{
int iNewIndex = m_iIndex == DATACENTER_CACHE - 1 ? 0 : m_iIndex + 1;
m_pData[iNewIndex] = rData;
m_iIndex = iNewIndex;
}
T& Read()
{
return m_pVector[m_iIndex];
}
void Set()
{
m_kCloneData = Read();
}
T& Get()
{
return m_kCloneData;
}
};
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation.
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)Set the event, to indicate that the thread is ready to receive posted messages.
看來,我們只需要在線程初始化時調一句PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)就可以了,然后在主線程中如此這般:
switch ( uMsg )
{
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
{
m_pLogic->StopThread();
WaitForSingleObject( m_pLogic->GetThreadHandle(), INFINITE );
PostQuitMessage(0);
}
break;
default:
{
if ( IsLogicMsg( uMsg ) )
{
PostThreadMessage( m_pLogic->GetThreadID(), uMsg, wParam, lParam );
}
else
{
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
}
break;
}
MSG msg;
while ( m_bRunning )
{
if ( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if ( ! GetMessageW( &msg, NULL, 0, 0 ) )
{
return (int) msg.wParam;
}
MessageProc( msg.message, msg.wParam, msg.lParam );
}
LogicTick();
}
struct VectorData 
{
Vector4f m_pVector[DATACENTER_CACHE];
int m_iIndex;

VectorData()
{
memset( m_pVector, 0, DATACENTER_CACHE * sizeof(Vector4f) );
m_iIndex = 0;
}

void Write( Vector4f& rVector )
{
int iNewIndex = m_iIndex == DATACENTER_CACHE - 1 ? 0 : m_iIndex + 1;
m_pVector[iNewIndex] = rVector;
m_iIndex = iNewIndex;
}

Vector4f& Read()
{
return m_pVector[m_iIndex];
}
};
class _DLL_Export Resource : public Base
{
public:
Resource();
virtual ~Resource();
// 是否過期
bool IsOutOfDate();

public:
// 是否就緒
virtual bool IsReady();
// 讀取資源
virtual bool Load();
// 釋放資源
virtual bool Release();
// 緩存資源
virtual bool Cache();
// 釋放緩存
virtual void UnCache();
protected:
// 加載標記
bool m_bLoad;
// 完成標記
bool m_bReady;

private:
};
template <class T>
class _DLL_Export ResHandle
{
public:
ResHandle()
{ m_pResource = NULL; }
virtual ~ResHandle()
{}
// 設置資源路徑
void SetPath( wstring szPath )
{
Resource * pResource = ResourceManager::GetSingleton()->GetResource( Key( szPath ) );
if ( pResource != NULL )
{
m_pResource = (T *) pResource;
}
else
{
m_pResource = new T;
m_pResource->SetPath( szPath );
ResourceManager::GetSingleton()->AddResource( m_pResource );
}
}
// 模板實體類指針
T * GetImpliment()
{ return (T *) m_pResource; }

T * operator-> ()
{ return (T *) m_pResource; }
protected:
// 模板實體類指針
Resource * m_pResource;
private:
};