我有一個我喜歡用來處理傳入數據的環形緩衝區實現。考慮到需要同步的情況下,以下方法是否安全高效?用於數據收集的RingBuffer
void CMyDlg::MyButton1()
{
RingBuffer BufRing(10000);
unsigned char InputBuf[100];
unsigned char OutBuf[100];
for (int ii = 0; ii < 1000; ++ii)
{
for (int i = 0; i < 100; ++i)
{
InputBuf[i] = i;
}
BufRing.Write(InputBuf,100);
BufRing.Read(OutBuf,100);
AfxBeginThread(WorkerThreadProc,OutBuf,THREAD_PRIORITY_NORMAL,0,0,NULL);
}
}
UINT WorkerThreadProc(LPVOID Param)
{
unsigned char* pThreadBuf = (unsigned char*)Param;
for (int c = 0; c < 100; ++c)
{
TRACE("Loop %d elemnt %x\n",c,pThreadBuf[c]);
}
return TRUE;
}
,如果你表現出RingBuffer實行這個問題會更有意義。它是否包含內部同步?它是否將輸入緩衝區複製到自身中,或者簡單地維護一個指向緩衝區的指針? –