접기
..
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
// <
#include <afxmt.h>
// >
#endif // !defined(AFX_STDAFX_H__FDE11EDF_10C2_45B7_9039_DC9CE6E9CA6F__INCLUDED_)
int g_nData[10][10];
int g_nHead = 0;
int g_nTail = 0;
int g_nCount = 0;
CCriticalSection g_csQueue;
CCriticalSection g_csBuffer[10];
CEvent g_evNotFull;
CEvent g_evNotEmpty;
CWinThread *g_pThread1, *g_pThread2;
volatile BOOL g_bContinue = TRUE;
const int NUMBER_OF_THREADS = 2;
HANDLE g_hThreads[NUMBER_OF_THREADS];
BOOL CEventDlg::OnInitDialog()
{
..
SetIcon(m_hIcon, FALSE); // Set small icon
g_pThread1 = AfxBeginThread(Thread_for_DataReceive, this);
g_pThread1->m_bAutoDelete = false;
g_pThread1->ResumeThread();
g_hThreads[0] = g_pThread1->m_hThread;
g_pThread2 = AfxBeginThread(Thread_for_DataDraw, this);
g_pThread2->m_bAutoDelete = false;
g_pThread2->ResumeThread();
g_hThreads[1] = g_pThread2->m_hThread;
return TRUE; // return TRUE unless you set the focus to a control
}
..
UINT Thread_for_DataReceive(LPVOID pParam)
{
CEventDlg *pWnd = (CEventDlg *)pParam;
int index;
while(g_bContinue)
{
Sleep(0);
while(1)
{
g_csQueue.Lock ();
if(g_nCount == 10)
{
g_csQueue.Unlock ();
g_evNotFull.Lock ();
continue;
}
break;
}
g_csBuffer[g_nHead].Lock ();
index = g_nHead;
g_nHead = (g_nHead+1)%10;
g_nCount++;
g_csQueue.Unlock ();
for(int i=0 ; i<10 ; i++)
g_nData[index][i] = rand()%100;
g_csBuffer[index].Unlock ();
g_evNotEmpty.PulseEvent ();
}
return 0;
}
UINT Thread_for_DataDraw(LPVOID pParam)
{
CEventDlg *pWnd = (CEventDlg *)pParam;
int index;
while(g_bContinue)
{
Sleep(0);
while(1)
{
g_csQueue.Lock ();
if(g_nCount == 0)
{
g_csQueue.Unlock ();
g_evNotEmpty.Lock ();
continue;
}
break;
}
g_csBuffer[g_nTail].Lock ();
index = g_nTail;
g_nTail = (g_nTail+1)%10;
g_nCount--;
g_csQueue.Unlock ();
..
g_csBuffer[index].Unlock ();
g_evNotFull.PulseEvent ();
}
return 0;
}
접기