<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; </b></font></span>
<font size="6" color="#FF0000"><b>Practice for multi-thread(1)</b></font></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">กก</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><b>A kind of combination of reader-writer and bounded-buffer problem. </b></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;

#define FileOpenError 1

const int ReadingLength=20;
const int WritingLength=80;
const int MaxThreadCount=5;
const char* inFileNames[MaxThreadCount-1] = 
{
	&quot;c:\\inputFile1.txt&quot;, 
	&quot;c:\\inputFile2.txt&quot;,
	&quot;c:\\inputFile3.txt&quot;,
	&quot;c:\\inputFile4.txt&quot;
};
		
const char* outFileName= &quot;c:\\outputFile.txt&quot;;
const int MaxBufferLength=256;

int front=0;
int rear=0;
int counter=0;

HANDLE threads[MaxThreadCount];

int threadIndex[MaxThreadCount]={0,1,2,3,4};

bool finished[MaxThreadCount]={false};

FILE *in[MaxThreadCount-1], *out;

char buffer[MaxBufferLength];


DWORD WINAPI run(void* param);

void doWriting(int tid);
void doReading(int tid);
bool isFull();
bool isEmpty();

HANDLE sems[MaxThreadCount];

int main()
{
	for (int i=0; i&lt;MaxThreadCount-1; i++)
	{		
		if ((in[i]=fopen(inFileNames[i], &quot;r&quot;))==NULL)
		{
			cout&lt;&lt;&quot;unable to open file &quot;&lt;&lt;inFileNames[i]&lt;&lt;endl;
			exit(FileOpenError);
		}
	}

	
	if ((out=fopen(outFileName, &quot;w&quot;))==NULL)
	{
		cout&lt;&lt;&quot;unable to open output file &quot;&lt;&lt;outFileName&lt;&lt;endl;
		exit(FileOpenError);
	}

	for (i=0; i&lt;MaxThreadCount; i++)
	{
		threads[i]=CreateThread(NULL, 0, run, threadIndex+i, CREATE_SUSPENDED, NULL);
		sems[i]=CreateSemaphore(NULL, 0, 1, NULL);
	}
	

	cout&lt;&lt;&quot;Now begin run...\n&quot;;
	ReleaseSemaphore(sems[4], 1, NULL);
	for (i=0; i&lt;MaxThreadCount; i++)
	{
		ResumeThread(threads[i]);
	}
	bool canRun=true, flag;
	while (canRun)
	{
		flag=true;
		for (int i=0; i&lt;MaxThreadCount; i++)
		{
			if (!finished[i])
			{
				flag=false;
				break;
			}
		}
		canRun=!flag;
	}
	//fputc(0, out);
	//fclose(out);

	return 0;
}

bool isFull()
{
	return (front+1)%MaxBufferLength==rear;
}

bool isEmpty()
{
	return front==rear;
}


void doWriting(int tid)
{
	if (isEmpty())
	{
		return;
	}
	
	if (counter==15)
	{
		for (int i=0; i&lt;MaxThreadCount; i++)
		{
			finished[i]=true;
		}
	}
	for (; (front-rear)%MaxBufferLength&gt;0; ++rear%=MaxBufferLength)
	{
		fputc(buffer[(rear+1)%MaxBufferLength], out);
	}
	ReleaseSemaphore(sems[4], 1, NULL);
	counter++;
}

void doReading(int tid)
{
	for (int i=0; i&lt;ReadingLength; i++)
	{
		if (isFull())
		{
			break;
		}
		buffer[++front%=MaxBufferLength]=fgetc(in[tid-1]);
	}
	ReleaseSemaphore(sems[tid-1], 1, NULL);
}


DWORD WINAPI run(void* param)
{
	while (true)
	{
		WaitForSingleObject(sems[*((int*)(param))], INFINITE);
		if (*((int*)(param))==0)
		{
			doWriting(*((int*)(param)));
		}
		else
		{
			doReading(*((int*)(param)));
		}
		if (finished[*((int*)(param))])
		{
			break;
		}
	}
	return *((int*)(param));
}



</pre>
<pre><font color="#0000FF"><b><span lang="en-ca">The input is something like following:</span></b></font></pre>
<pre><font color="#0000FF"><b>The input file is 4 files:</b></font></pre>
<pre>this is input file 1this is input file 1this is input file 1....</pre>
<pre>this is input file 2this is input file 2this is input file 2this is input file 2...</pre>
<pre>this is input file 3this is input file 3this is input file 3this is input file 3...</pre>
<pre>this is input file 4this is input file 4this is input file 4this is input file 4this is input file 4...</pre>
<pre><font color="#0000FF"><b>Here is the <a name="result"></a>result:</b></font></pre>

<p>this is input file 4this is input file 3this is input file 2this is input 
file 1this is input file 4this is input file 3this is input file 2this is input 
file 1this is input file 4this is input file 3this is input file 2this is input 
file 1</p>

<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>