<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>CFGReader</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
multiple reader</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>I am practicing with multi-thread.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">Actually this is my way of learning. I read 
  the MSDN and see the example of &quot;WaitForMultipleObjects&quot; API and</span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">try to do a simple practice by creating one 
  &quot;write&quot; thread and multiple &quot;read&quot; thread. At beginning, I just</span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">copy the idea of example by declare one 
  single &quot;write event&quot; but when running, I realized that reader must alos</span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">notify the &quot;writer&quot; when he finished 
  reading. Then I have to give each reader two events or semaphore, one for</span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">reader to begin reading, one for writer to 
  know that reading is finished. </span></div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<div align="left">
  <pre><span lang="en-ca">I am not sure about the difference of &quot;Event&quot; and &quot;Semaphore&quot;. It seems to me the &quot;auto-reset&quot; event is just same as semaphore,</span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">right?</span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. main.cpp (main)</span></b></font></pre>
  <pre>กก</pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: </b></font></span><font size="3" color="#FF0000"><b><span lang="en-ca">main</span></b></font><span lang="en-ca"><font size="3" color="#FF0000"><b>.h</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;windows.h&gt;

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, &quot;.txt&quot;);
	bFinished = false;
	hWriteThread=CreateThread(NULL, 0, writeProc, NULL, 0, NULL);
	
	for (int i=0; i&lt;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, &quot;writeEvent&quot;);
		hReadThread[i]=CreateThread(NULL, 0, readProc, (void*)(index+i), 0, NULL);
		pFiles[i]=fopen(fileName, &quot;w&quot;);
	}

}

DWORD WINAPI writeProc(void* param)
{
	while (counter&lt;20)
	{
		/*
		for (int i=0; i&lt;MaxReaderCount; i++)
		{
			ResetEvent(hWriteEvent[i]);
		}
		*/
		//buffer[0]='\0';
		WaitForMultipleObjects(MaxReaderCount, hReadEvent, true, INFINITE);
		//cin&gt;&gt;buffer;
		gets(buffer);
		//SetEvent(hWriteEvent);
		for (int i=0; i&lt;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;
}




</pre>
<pre></pre>
<pre><font color="#0000FF"><b><span lang="en-ca">The input is something like following:</span></b></font></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>The result is like this:</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>In the console window, you are prompt to key in some craps. And after you typed 20 &quot;enter&quot; key, the program</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>finished and you find out that in the current directory there 10 garbage files appeared and each of them contains</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>same garbage you have type on screen.</b></font></span></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>