<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>Parallel</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Parallel Ranking List</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>
  <pre><b><span lang="en-ca">The problem is quite unusual, I mean unimaginable. Say we have a link list and suddenly we are becoming crazy to want to </span></b></pre>
  <pre><b><span lang="en-ca">dismantle our link list to make them parallel, for example, to save them in an array. However, before breaking the link</span></b></pre>
  <pre><b><span lang="en-ca">we would like to know their ranks or their position in the link list in order to put them in array in an appropriate</span></b></pre>
  <pre><b><span lang="en-ca">order. So, this is the question and more particularly, Professor Probst thinks we are rich enough to waste some</span></b></pre>
  <pre><span lang="en-ca"><b>resources to assign each node in list with a thread to solve the problem in a multi-thread manner. </b></span></pre>
  <pre><font color="#FF0000"><span lang="en-ca">The following is the original link list:</span></font></pre>
  <pre><span lang="en-ca"><b>[index:1, rank:1]--&gt;[index:2, rank:1]--&gt;[index:4, rank:1]--&gt;[index:0, rank:1]--&gt;[index:3, rank:1]</b></span></pre>
  <pre><font color="#FF0000"><span lang="en-ca">Finally you break the list to make them all at same level, say in an array with appropriate rank being calculated for each.</span></font></pre>
  <pre><span lang="en-ca"><b>now print the result
index:0, randk:2
index:1, randk:5
index:2, randk:4
index:3, randk:1
index:4, randk:3
Press any key to continue</b></span></pre>
  <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><span lang="en-ca"><b>There are many ways to do this job and for multi-threading environment, you have to have necessary protection of </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>critical section. See one node's rank can be easily calculated by adding its next node's rank to itself provided</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>that its next node is not writing its rank. That is to say, you must make sure that adding rank of your next node</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>would not be concurrent with changing the position of your next node. Then the rank you are adding is sure the </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>correct one. Using a simple lock to ensure it.</b></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>
</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;time.h&gt;
#include &lt;windows.h&gt;

using namespace std;

const int MaxListNode=20;

struct Node
{
	int rank;
	int index;
	Node* next;
};

Node nodes[MaxListNode];
bool added[MaxListNode]={false};

Node* head=NULL, *tail=NULL;

HANDLE worker[MaxListNode];

void initialize();

void addNode(int index);

void printList();

DWORD WINAPI run(void* param);

HANDLE mutex;
int counter=0;

int demon[MaxListNode];

void interrupt(int index);

int main()
{
	srand(time(0));
	initialize();
	printList();
	for (int i=0; i&lt;MaxListNode; i++)
	{
		ResumeThread(worker[i]);
	}
	while (counter&lt;MaxListNode)
	{
		Sleep(0);
	}
	cout&lt;&lt;&quot;now print the result\n&quot;;
	for (i=0; i&lt;MaxListNode; i++)
	{
		cout&lt;&lt;&quot;index:&quot;&lt;&lt;nodes[i].index&lt;&lt;&quot;, randk:&quot;&lt;&lt;nodes[i].rank&lt;&lt;endl;
	}

	return 0;
}

void printList()
{
	Node* ptr=head;
	while (ptr!=NULL)
	{
		cout&lt;&lt;&quot;index:&quot;&lt;&lt;ptr-&gt;index&lt;&lt;&quot;, rank:&quot;&lt;&lt;ptr-&gt;rank&lt;&lt;&quot;\n&quot;;
		ptr=ptr-&gt;next;
	}
}


void addNode(int index)
{
	if (head==NULL)
	{
		head=tail=&amp;nodes[index];
	}
	else
	{
		tail-&gt;next=&amp;nodes[index];
		tail=&amp;nodes[index];
	}
}

void initialize()
{
	int count=0, index;
	for (int i=0; i&lt;MaxListNode; i++)
	{
		nodes[i].index=i;
		nodes[i].rank=1;
		nodes[i].next=NULL;
		demon[i]=rand()%10;
		worker[i]=CreateThread(NULL, 0, run, (void*)&amp;nodes[i], CREATE_SUSPENDED , NULL);
	}
	while (count&lt;MaxListNode)
	{
		index=rand()%MaxListNode;
		if (added[index])
		{
			continue;
		}
		added[index]=true;
		addNode(index);
		count++;
	}
	mutex=CreateMutex(NULL, false, NULL);
	
}

bool acquire(Node* ptr)
{
	WaitForSingleObject(mutex, INFINITE);
	if (ptr==NULL)
	{
		ReleaseMutex(mutex);
		return true;
	}
	int index=ptr-&gt;index;
	if (added[index])//means old
	{
		added[index]=false;
		ReleaseMutex(mutex);
		return true;
	}
	//means it is locked: false
	ReleaseMutex(mutex);
	return false;
}

void release(Node* ptr)
{
	WaitForSingleObject(mutex, INFINITE);
	if (ptr!=NULL)
	{		
		added[ptr-&gt;index]=true;		
	}
	ReleaseMutex(mutex);
}

void interrupt(int index)
{
	if (demon[index]&lt;4)
	{
		Sleep(0);
	}
}

DWORD WINAPI run(void* param)		
{
	while (true)
	{
		if (acquire((Node*)param))
		{
			//WaitForSingleObject(mutex, INFINITE);
			interrupt(((Node*)param)-&gt;index);
			Node* ptr=((Node*)param)-&gt;next;
			interrupt(((Node*)param)-&gt;index);
			if (acquire(ptr))
			{
				interrupt(((Node*)param)-&gt;index);
				if (ptr==NULL)
				{					
					counter++;
					release(ptr);
					release((Node*)param);
					break;				
				}
				else
				{
					((Node*)param)-&gt;rank+=ptr-&gt;rank;
					((Node*)param)-&gt;next=ptr-&gt;next;	
					
				}
				interrupt(((Node*)param)-&gt;index);
				release(ptr);				
			}
			interrupt(((Node*)param)-&gt;index);
			release((Node*)param);
		}
		Sleep(0);
	}
	return 0;
}
			


</pre>
<pre>กก</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>

<pre>index:16, rank:1
index:11, rank:1
index:7, rank:1
index:12, rank:1
index:6, rank:1
index:13, rank:1
index:14, rank:1
index:8, rank:1
index:1, rank:1
index:17, rank:1
index:10, rank:1
index:2, rank:1
index:18, rank:1
index:0, rank:1
index:15, rank:1
index:5, rank:1
index:9, rank:1
index:19, rank:1
index:3, rank:1
index:4, rank:1
now print the result
index:0, randk:7
index:1, randk:12
index:2, randk:9
index:3, randk:2
index:4, randk:1
index:5, randk:5
index:6, randk:16
index:7, randk:18
index:8, randk:13
index:9, randk:4
index:10, randk:10
index:11, randk:19
index:12, randk:17
index:13, randk:15
index:14, randk:14
index:15, randk:6
index:16, randk:20
index:17, randk:11
index:18, randk:8
index:19, randk:3
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>