<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="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Why 
Java?Why not C++?</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><font size="3">This is first<span lang="en-ca"> edition of my one-to-one translation of programming assignment of comp346 from Java</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>to C++.</b></font></span></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">
  <b>In Comp3<span lang="en-ca">46</span> we <span lang="en-ca">are forced to 
  learn the idiotic programming language of Java and I am personally </span> </b></div>
<div align="left">
  　</div>
<div align="left">
  <b><span lang="en-ca">against this stupid decision of CS department of 
  Concordia though it might be a benefit for students</span></b></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>to find a job. However, in my view it will poison 
  student's brain by those idiotic ideas.</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>The following is the stupid code of multi-threading java 
  code.</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <pre>public class Future {
  static int length = 9;                            // 1-indexed semaphores
  static int number = 8;                            // 0-indexed workers 
  static int sum    = 0;

  static Daemon daemon = new Daemon ();
  static Semaphore future[] = new Semaphore[length];
  static int arr[] = new int[] {0, -1, -1, -1, -1, -1, -1, -1, +1};

  public static void main (String[] a) {

    Worker w[] = new Worker[number];

    for( int j=0; j&lt;length; j++ )    // definition before use
      future[j] = new Semaphore (0);

    for( int j=0; j&lt;number; j++ ) {
      w[number-j-1] = new Worker (number-j);
      w[number-j-1].start ();
    }

    System.out.println (&quot;Eight workers have started.&quot;);
    System.out.println (&quot;Main thread continues work.&quot;);
    for( int j=0; j&lt;number; j++ ) {
      System.out.println (&quot;Main thread waits on semaphore &quot; + (j+1) + &quot;.&quot;);
      future[j+1].Wait ();
      System.out.println (&quot;Main thread reads &quot; + arr[j+1] + &quot; from position &quot;
                         + (j+1) + &quot;.&quot;);
      sum = sum + arr[j+1];
    }
    System.out.println (&quot;Sum = &quot; + sum);
    for( int j=0; j&lt;number; j++ ) {                 // wait for workers
      try { w[j].join (); }                         // to terminate
      catch (InterruptedException e) { };
    }
    System.out.println (&quot;System terminates normally.&quot;);
  }
}

class Daemon {
  private static int d[] = new int[] {0, 4, 4, 4, 6, 4, 6, 4, 6};

  public synchronized void interrupt (int tid) {
    if ( d[tid] &gt; 4 )
    {
      System.out.println(&quot;worker &quot;+tid+ &quot; is interrupted.&quot;);	
      Thread.yield ();
    }
  }
}

class Semaphore {
  private int value;
  Semaphore (int value1) {
    value = value1;
  }

  public synchronized void Wait () {
    while( value &lt;= 0 ) {
      try { wait (); }
      catch (InterruptedException e) { };
    }
    value--;
  }

  public synchronized void Signal () {
    ++value;
    notify ();
  }
}

class Worker extends Thread {
  private int tid;
  Worker (int tid1) {
    tid = tid1;
  }

  public void run () {
    System.out.println (&quot;Worker &quot; + tid + &quot; begins execution.&quot;);
    yield ();
    System.out.println (&quot;Worker &quot; + tid + &quot; doubles own array component.&quot;);
    Future.arr[tid] = 2 * Future.arr[tid];
    Future.daemon.interrupt (tid);
    System.out.println (&quot;Worker &quot; + tid + &quot; sets neighbor's array component.&quot;);
    Future.arr[tid-1] = 1;
    System.out.println (&quot;Worker &quot; + tid + &quot; signals semaphore &quot; + tid + &quot;.&quot;);
    Future.future[tid].Signal ();
    System.out.println (&quot;Worker &quot; + tid + &quot; terminates.&quot;);
  }
}
</pre>
  <p>　</div>
<div 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">
  <span lang="en-ca"><b>There is almost all equivalent functions to use in C++ 
  except that you have to use different function</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>to be equivalent to Java's &quot;notify&quot;, depending what 
  synchronized objects you are using, like mutex,</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>semaphore etc. </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>1. However, there is indeed a little difficulty about &quot;cout&quot;. 
  You have to sychronized it</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>by yourself otherwise different threads might mess up 
  all messages. So, I designed a mutex for all</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>thread, including main thread, to wait for it before 
  output and release it after displaying.</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>This method has a potential effect that it divides the 
  programming execution by all &quot;displaying&quot; </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>methods. It means that your thread will not be able to 
  be &quot;atomic&quot; across &quot;display&quot; unless you use </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>a higher level &quot;critical region&quot; method to bind them 
  into an atomic part.</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>2. By changing array of &quot;daemon&quot; you will get random 
  result ranging from 9 to 16.</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>3. The professor's mistake on Java is to make the 
  &quot;interrupt&quot; method of Daemon class to be </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>&quot;synchronized&quot; which makes the function &quot;interrupt&quot; an 
  atomic. The immediate effect is that all</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>threads are blocked before &quot;interrupt&quot; after first 
  interruption happens. And because the &quot;yield&quot; is</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>within the &quot;interrupt&quot; function, it won't return until 
  the thread get next chance to run CPU. So, </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>all threads are queuing up before &quot;interrupt&quot;, and the 
  &quot;interrupt&quot; doesn't function like a </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>&quot;interrupt&quot; at all. This is explained perfectly
  <a href="Chaoes.htm#wonderful">clear by Mr. Mokhov</a>. What a wonderful 
  explanation!</b></span></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><span lang="en-ca"><font size="3"><b>1. semaphore.cpp (main)</b></font></span></pre>
</div>
<pre></pre>
<pre><font size="3" color="#FF0000"><span lang="en-ca"><b>file name: semaphore</b></span><b>.cpp (main)</b></font>
</pre>
<pre>#include &lt;iostream&gt;
#include &lt;windows.h&gt;

using namespace std;

const int ThreadCount=8;

int array[ThreadCount+1]={1,-1,-1,-1,-1,-1,-1,-1,-1};

HANDLE mutex;

int params[ThreadCount]={0,1,2,3,4,5,6,7};

int daemon[ThreadCount]={1,1,5,1,6,1,6,1};

DWORD WINAPI run(LPVOID param);

HANDLE Workers[ThreadCount];

HANDLE semaphores[ThreadCount];

void interrupt(int i);

void display(char* str, int i);

int main()
{	
	int sum=0;
	//this is mutex for cout, otherwise output will be messed up.
	mutex=CreateMutex(NULL, false, NULL);
	//create semaphores and initialized to unsignaled
	for (int i=0; i&lt;ThreadCount; i++)
	{
		semaphores[i]=CreateSemaphore(NULL, 0, 1, NULL);
	}
	//create threads with suspended and in descending order of index
	for (i=ThreadCount-1; i&gt;=0; i--)
	{
		Workers[i]=CreateThread(NULL, 0, run, &amp;params[i], CREATE_SUSPENDED , NULL);		
	}
	display(&quot;Eight worker have started&quot;, -1);
	display(&quot;Main thread continues to work&quot;, -1);
	
	//equivalent to &quot;start&quot;
	for (i=0; i&lt;ThreadCount; i++)
	{
		ResumeThread(Workers[i]);
	}

	for (i=0; i&lt;ThreadCount; i++)
	{		
		display(&quot;main thread waits on semaphore &quot;, i);			
		
		WaitForSingleObject(semaphores[i], INFINITE);
		
		display(&quot;main thread reads  &quot;, array[i]);
		
		sum+=array[i];
	}
	display(&quot;the sum is &quot;, sum);

	return 0;
}

void display(char* str, int i)
{
	WaitForSingleObject(mutex, INFINITE);

	cout&lt;&lt;str;
	if (i!=-1)
	{
		cout&lt;&lt;i;
	}
	cout&lt;&lt;&quot;\n&quot;;
	ReleaseMutex(mutex);
}


void interrupt(int i)
{
	if (daemon[i]&gt;4)
	{
		display(&quot;interrupt of worker &quot;, i);
		Sleep(0);
	}
}

//if you want to get result of 16, you have to comment out all
//display() because in order to &quot;cout&quot; properly, I used a &quot;mutex&quot;
//to block all concurrent &quot;cout&quot; within &quot;display&quot;.
//So, you see all &quot;displays&quot; &quot;divides&quot; executions into independent 
//parts, therefore the output is always 9 unless displays are commented out.
DWORD WINAPI run(LPVOID parameter)
{
	//at first I thought I cannot declare local variable to replace this 
	//but finally I understand that thread has its own stack and some 
	//local variable, right?
	int i=*((int*)(parameter));
	
	display(&quot; begins execution of worker of &quot;, i);
	//display(&quot; begins execution of worker of &quot;, i);
	Sleep(0);
	//display(&quot; doubles own array component of worker of &quot;, i);
	
	array[i]*=2;
	//array[*((int*)(parameter))]*=2;//
	

	//the Professor's mistake is to add &quot;synchronized&quot; in front of 
	//interrupt, so it is equivalent to add a &quot;wait&quot; here
	//and a release &quot;mutex&quot; or &quot;semaphore after &quot;interruption&quot;
	//This is explained very clearly by Mr. Mokhov and I learned it from 
	//him. Well done!

	interrupt(i);
	//interrupt(*((int*)(parameter)));	
	//display(&quot; set neighbour's array component of worker &quot;, i);

	array[i+1]=1;
	//array[*((int*)(parameter))+1]=1;

	//display(&quot;send semaphore to &quot;, i);

	ReleaseSemaphore(semaphores[i], 1, NULL);	
	//display(&quot;terminates of worker of &quot;, i);

	return i;
}


</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><font color="#0000FF"><b>Here is the result:<span lang="en-ca"> </span></b></font></pre>

<pre>Eight worker have started
Main thread continues to work
begins execution of worker of 0
begins execution of worker of 1
begins execution of worker of 2
begins execution of worker of 3
begins execution of worker of 4
begins execution of worker of 5
begins execution of worker of 6
begins execution of worker of 7
main thread waits on semaphore 0
interrupt of worker 2
interrupt of worker 4
interrupt of worker 6
main thread reads 2
main thread waits on semaphore 1
main thread reads 2
main thread waits on semaphore 2
main thread reads 2
main thread waits on semaphore 3
main thread reads 1
main thread waits on semaphore 4
main thread reads 2
main thread waits on semaphore 5
main thread reads 1
main thread waits on semaphore 6
main thread reads 2
main thread waits on semaphore 7
main thread reads 1
the sum is 13
Press any key to continue
</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>