<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>Sleeping barber</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">
  <pre><b>A barber's shop have n seat for customers to be seated while waiting. One barber's seat for customer to be seated</b></pre>
</div>
<div align="left">
  <pre><b>while cutting hair and also for barber to sleep in while no customer is waiting. Customer comes and first try to </b></pre>
</div>
<div align="left">
  <pre><b>sit if there is free seat, otherwise just leaves. If barber is sleeping, customer must wake up barber. If barber</b></pre>
</div>
<div align="left">
  <pre><b>is cutting hair for some customer, the seated customers just wait in seats.</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>I use 10 threads to represent 10 customers while there are only 5 seats in barber's shop for waiting. And for </b></pre>
</div>
<div align="left">
  <pre><b>each customer, there is one state variable associated with him, same for barber. There are two &quot;mutex&quot;, one for</b></pre>
</div>
<div align="left">
  <pre><b>waiting seat, one for cutting hair which are all critical area. Customer comes and enter mutex of &quot;seat&quot; to see</b></pre>
</div>
<div align="left">
  <pre><b>if there are empty seat to sit in. If yes, he sits and begin waiting in queue of barber's turn for hair-cutting</b></pre>
</div>
<div align="left">
  <pre><b>by entering mutex of &quot;barber&quot;. There is only one small trivial problem for the global variable &quot;seatCount&quot; which</b></pre>
</div>
<div align="left">
  <pre><b>is used to represent for how many waiting customers there are. It can be incremented when customer comes. And it </b></pre>
</div>
<div align="left">
  <pre><b>also can be decremented when barber finished servicing a customer. Incrementing is no problem because it is inside</b></pre>
</div>
<div align="left">
  <pre><b>of &quot;seatMutex&quot;, however, barber doesn't enter this mutex. So, there may be some short intervals while &quot;seatCount&quot;</b></pre>
</div>
<div align="left">
  <pre><b>is not accurate. But does anybody really refer to this data? I don't see it at least in this simple case. So, I </b></pre>
</div>
<div align="left">
  <pre><b>just leave it there.</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><span lang="en-ca"><b>One of big problem is synchronization with &quot;std cout&quot;. You see, my thread is messed up when they use &quot;cout&quot; for</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>display. I guess the reason is that I/O is extremely slow and context switch between threads is not synchronized</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>with output. So, I have to store all result in an array called &quot;log&quot;. And display it only after threads terminate.</b></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>
</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;windows.h&gt;
#include &lt;iostream&gt;

using namespace std;


const int TotalCustNum=10;
const int SeatNum=5;

int custCount=0;
int seatCount=0;
int barIndex=TotalCustNum;
int custIndex[TotalCustNum];
int log[500];
int logCount=0;

HANDLE barbarMutex, seatMutex, display;
HANDLE customers[TotalCustNum];
//HANDLE barbar;

enum State
{Sleeping, Serving, Waiting, Leaving, Cutting};
char* stateStr[5]={&quot;Sleeping&quot;, &quot;Serving&quot;, &quot;Waiting&quot;, &quot;Leaving&quot;, &quot;Cutting&quot;};

State states[TotalCustNum+1];

int daemon[TotalCustNum];

int servedCount=0;

void interrupt(int i);

void initialize();

DWORD WINAPI run(void* param);

void beServed(int i);

int main()
{
	initialize();
	while (servedCount&lt;20)
	{
		WaitForSingleObject(display, INFINITE);
		for (int i=0; i&lt;TotalCustNum; i++)
		{
			log[logCount++]=states[i];
		}
		ReleaseMutex(display);
		Sleep(0);
	}
	for (int i=0; i&lt;TotalCustNum; i++)
	{
		CloseHandle(customers[i]);
	}
	i=0;
	while (i&lt;logCount)
	{
		for (int j=0; j&lt;TotalCustNum; j++)
		{
			cout&lt;&lt;&quot;customer &quot;&lt;&lt;j&lt;&lt;&quot; is &quot;&lt;&lt;stateStr[log[i++]]&lt;&lt;&quot;\n&quot;;
		}
		cout&lt;&lt;&quot;round of &quot;&lt;&lt;i/TotalCustNum&lt;&lt;endl;
		
	}
	return 0;
}

void initialize()
{
	barbarMutex=CreateMutex(NULL, false, NULL);
	seatMutex=CreateMutex(NULL, false, NULL);
	display=CreateMutex(NULL, false, NULL);
//	barbar=CreateThread(NULL, 0, run, &amp;barIndex, 0, NULL);
	states[barIndex]=Sleeping;
	for (int i=0; i&lt;TotalCustNum; i++)
	{
		custIndex[i]=i;
		daemon[i]= rand()%TotalCustNum;
		states[i]=Leaving;
		customers[i]=CreateThread(NULL, 0, run, custIndex+i, 0, NULL);
	}
}
	
void beServed(int i)
{
	WaitForSingleObject(barbarMutex, INFINITE);
	if (states[barIndex]==Sleeping)
	{
		WaitForSingleObject(display, INFINITE);
		states[barIndex]=Serving;
		ReleaseMutex(display);
	}
	states[i]=Cutting;
	Sleep(0);
	//here is a little small problem, since
	//seatCount might be changed at same time by newly-arrived customer,
	//but it does little harm, since, barbar's state is not used by anyone
	//and will be corrected when customer having hair-cut.
	seatCount--;
	if (seatCount==0)
	{
		WaitForSingleObject(display, INFINITE);
		states[barIndex]=Sleeping;
		ReleaseMutex(display);
	}
	WaitForSingleObject(display, INFINITE);
	states[i]=Leaving;
	ReleaseMutex(display);
	servedCount++;
	ReleaseMutex(barbarMutex);
	
}

void interrupt(int i)
{
	if (daemon[i]&lt;TotalCustNum/2)
	{
		Sleep(0);
	}
}

DWORD WINAPI run(void* param)
{
	while (servedCount&lt;20)
	{
		interrupt(*((int*)param));
		if (WaitForSingleObject(seatMutex, 0)==WAIT_OBJECT_0)
		{
			if (seatCount&lt;SeatNum)
			{
				seatCount++;
				WaitForSingleObject(display, INFINITE);
				states[*((int*)param)]=Waiting;
				ReleaseMutex(display);
				ReleaseMutex(seatMutex);
				beServed(*((int*)param));
			}
			else
			{
				//still Leaving
				ReleaseMutex(seatMutex);
			}
		}
	}
	return servedCount;
}


</pre>
<pre></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>Here is the <a name="result"></a>result:</b></font></pre>

<p>customer 0 is Leaving<br>
customer 1 is Leaving<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Leaving<br>
customer 5 is Leaving<br>
customer 6 is Leaving<br>
customer 7 is Leaving<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 1<br>
customer 0 is Leaving<br>
customer 1 is Cutting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 2<br>
customer 0 is Waiting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Cutting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 3<br>
customer 0 is Waiting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Cutting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 4<br>
customer 0 is Waiting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Cutting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 5<br>
customer 0 is Cutting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 6<br>
customer 0 is Leaving<br>
customer 1 is Cutting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 7<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Cutting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 8<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Cutting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 9<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Cutting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 10<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Cutting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 11<br>
customer 0 is Waiting<br>
customer 1 is Cutting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 12<br>
customer 0 is Waiting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Cutting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 13<br>
customer 0 is Waiting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Cutting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 14<br>
customer 0 is Waiting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Cutting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 15<br>
customer 0 is Cutting<br>
customer 1 is Waiting<br>
customer 2 is Leaving<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 16<br>
customer 0 is Leaving<br>
customer 1 is Cutting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 17<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Cutting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 18<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Cutting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 19<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Waiting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Cutting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 20<br>
customer 0 is Leaving<br>
customer 1 is Waiting<br>
customer 2 is Cutting<br>
customer 3 is Leaving<br>
customer 4 is Waiting<br>
customer 5 is Leaving<br>
customer 6 is Waiting<br>
customer 7 is Waiting<br>
customer 8 is Leaving<br>
customer 9 is Leaving<br>
round of 21<br>
Press any key to continue</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>