'Reader Writer'에 해당되는 글 1건

  1. 2010.01.08 Reader / Writer 동기화

Reader / Writer 동기화

CS/Common 2010. 1. 8. 05:45
원문 : Stanford iTunesU Programming Paradigm 16강

버퍼에서 Read 전에 Write 금지.
Write 되지 않은 버퍼 Read 하지 않기.
char buffer[8];
Semaphore emptyBuffers(8);
Semaphore fullBuffers(0);

int main()
{
	ThreadNew("Writer", Writer, 0);
	ThreadNew("Reader", Reader, 0);
	RunAllThread();
}

void Writer()
{
	for (int i=0; i < 40; ++i)
	{
		char c = PrepareRandomChar();
		SemaphoreWait(emptyBuffers);
		buffer[ i%f ] = c;
		SemaphoreSignal(fullBuffers);
	}
}

void Reader()
{
	for (int i=0; i < 40; ++i)
	{
		SemaphoreWait(fullBuffers);
		char c = buffer[ i%f ];
		ProcessChar(c);
		SemaphoreSignal(emptyBuffers);
	}
}
: