빠르고 유익한 Win32 솔루션에는 UI 섹션에 임계 구역, 텍스트 버퍼 및 타이머가 포함됩니다.
몇 전역을 정의
...
void onTimer()
{
if (overflow)
{
// handle this
}
else
if (newdata) // new data to display
{
// take the lock, copy the data and release the lock quickly
EnterCriticalSection(&bufferLock);
int dataread = newdata;
memcpy(uiDataBuffer, dataBuffer, dataread);
newdata = 0;
LeaveCriticalSection(&bufferLock);
// TODO: append the text in uiDataBuffer[] to your text control
}
}
는 작업자 스레드에서 호출 할
CRITICAL_SECTION bufferLock; // critical section (to be initialized somewhere)
char dataBuffer[65536]; // contains the data that will be sent to the form
int newdata = 0; // how much data we got (this variable must be atomic, int is ok)
char uiDataBuffer[65536]; // data available to the form
int overflow = 0; // just in case...
UI 스레드 타이머 : 검증되지 않은
void sendData (char* data, int size)
{
EnterCriticalSection (&bufferLock);
if(size+newdata > 65536)
overflow = 1;
else
{
memcpy(dataBuffer+newdata, data, size);
newdata += size;
}
LeaveCriticalSection (&bufferLock);
}
코드입니다. 버퍼 크기 및 타이머 빈도를 조정해야합니다.
PostMessage() (사용자 지정 메시지 사용)를 사용하여 새 데이터를 사용할 수 있음을 UI에 알리기 위해 타이머를 사용하여 버퍼를 폴링하는 것을 피할 수 있습니다.
성능이 문제가되면 잠금없는 FIFO 대기열을 사용하여 생산자와 소비자 스레드 간의 데이터 교환을 매우 효율적으로 수행 할 수 있습니다.
PostMessage() 혼자 은 스레드 간 데이터 교환을위한 해결책이 아닙니다.
보조 스레드의 대화 상자에 맞춤 메시지를 게시하십시오. – ildjarn