CFGReader

multiple reader

    A. First Edition

    I am practicing with multi-thread.

    B.The problem

Actually this is my way of learning. I read the MSDN and see the example of "WaitForMultipleObjects" API and

try to do a simple practice by creating one "write" thread and multiple "read" thread. At beginning, I just

copy the idea of example by declare one single "write event" but when running, I realized that reader must alos

notify the "writer" when he finished reading. Then I have to give each reader two events or semaphore, one for

reader to begin reading, one for writer to know that reading is finished.

C.The idea of program
กก

    I am not sure about the difference of "Event" and "Semaphore". It seems to me the "auto-reset" event is just same as semaphore,

    right?

    D.The major functions

    E.Further improvement

    F.File listing

    1. main.cpp (main)

    กก

    กก

    file name: main.h

    #include <iostream>
    #include <windows.h>

    using namespace std;

    const int MaxReaderCount=10;
    const int MaxBufferLength=256;

    HANDLE hReadEvent[MaxReaderCount];
    HANDLE hReadThread[MaxReaderCount];
    HANDLE hWriteEvent[MaxReaderCount];
    HANDLE hWriteThread;
    int index[MaxReaderCount];
    int counter=0;
    char buffer[MaxBufferLength];
    bool bFinished;
    FILE* pFiles[MaxReaderCount];

    void initialize();
    DWORD WINAPI writeProc(void* param);
    DWORD WINAPI readProc(void* param);

    int main()
    {
    	initialize();
    	while (!bFinished)
    	{

    		Sleep(0);
    	}

    	return 0;
    }

    void initialize()
    {
    	char threadName[10]={'t','h','r','e','a','d','*','\0'};
    	char fileName[20];
    	strcpy(fileName, threadName);
    	strcat(fileName, ".txt");
    	bFinished = false;
    	hWriteThread=CreateThread(NULL, 0, writeProc, NULL, 0, NULL);

    	for (int i=0; i<MaxReaderCount; i++)
    	{
    		threadName[6]='0'+i;
    		fileName[6]='0'+i;
    		index[i]=i;
    		hReadEvent[i]=CreateEvent(NULL, false, true, threadName);
    		hWriteEvent[i]=CreateEvent(NULL, false, false, "writeEvent");
    		hReadThread[i]=CreateThread(NULL, 0, readProc, (void*)(index+i), 0, NULL);
    		pFiles[i]=fopen(fileName, "w");
    	}

    }

    DWORD WINAPI writeProc(void* param)
    {
    	while (counter<20)
    	{
    		/*
    		for (int i=0; i<MaxReaderCount; i++)
    		{
    			ResetEvent(hWriteEvent[i]);
    		}
    		*/
    		//buffer[0]='\0';
    		WaitForMultipleObjects(MaxReaderCount, hReadEvent, true, INFINITE);
    		//cin>>buffer;
    		gets(buffer);
    		//SetEvent(hWriteEvent);
    		for (int i=0; i<MaxReaderCount; i++)
    		{
    			SetEvent(hWriteEvent[i]);
    		}
    		counter++;
    	}
    	bFinished=true;
    	return 0;
    }

    DWORD WINAPI readProc(void* param)
    {
    	//HANDLE local[2];
    	int i=*(int*)(param);
    	//local[0]=hWriteEvent;
    	//local[1]=hReadEvent[i];
    	while (!bFinished)
    	{
    		//WaitForMultipleObjects(2, local, true, INFINITE);
    		WaitForSingleObject(hWriteEvent[i], INFINITE);
    		fputc('\n', pFiles[i]);
    		fputc('0'+i, pFiles[i]);
    		fputc('\n', pFiles[i]);
    		fputs(buffer, pFiles[i]);
    		SetEvent(hReadEvent[i]);
    	}
    	fclose(pFiles[i]);
    	return 0;
    }

    The input is something like following:

    The result is like this:

    In the console window, you are prompt to key in some craps. And after you typed 20 "enter" key, the program

    finished and you find out that in the current directory there 10 garbage files appeared and each of them contains

    same garbage you have type on screen.