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)就可以了,然后在主線程中如此這般: