<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>中文 &#187; wgj200123</title>
	<atom:link href="http://software.intel.com/zh-cn/blogs/author/wgj200123/feed/" rel="self" type="application/rss+xml" />
	<link>http://software.intel.com/zh-cn/blogs</link>
	<description></description>
	<lastBuildDate>Sat, 26 May 2012 06:34:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>利用Windows API 多线程编程解决“哲学家进餐问题”</title>
		<link>http://software.intel.com/zh-cn/blogs/2012/01/13/windows-api/</link>
		<comments>http://software.intel.com/zh-cn/blogs/2012/01/13/windows-api/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 08:28:59 +0000</pubDate>
		<dc:creator>wgj200123</dc:creator>
				<category><![CDATA[博客征文专栏]]></category>
		<category><![CDATA[并行计算]]></category>

		<guid isPermaLink="false">http://software.intel.com/zh-cn/blogs/2012/01/13/windows-api/</guid>
		<description><![CDATA[哲学家进餐问题描述： 有五个哲学家，他们的生活方式是交替地进行思考和进餐。哲学家们公用一张圆桌，周围放有五把椅子，每人坐一把。在圆桌上有五个碗和五根筷子，当一个哲学家思考时，他不与其他人交谈，饥饿时便试图取用其左、右最靠近他的筷子，但他可能一根都拿不到。只有在他拿到两根筷子时，方能进餐，进餐完后，放下筷子又继续思考。 01.#include 02.#include 03. 04.#define P(S) WaitForSingleObject(S, INFINITE) //P操作 05.#define V(S) ReleaseSemaphore(S, 1, NULL) 06.#define NUMBERS 5 //叉子个数 07.typedef HANDLE Semaphore; //信号量原型 08.Semaphore chopstick[5]; 09.Semaphore mutex; //定义一个互斥量 10.Semaphore room; 11.char *philosophers[5] = {"哲学家1 ", "哲学家2 ", "哲学家3 ", "哲学家4 ", "哲学家5 "}; 12.char *str[] = {"得到左手边的筷子", "得到右手边的筷子", "得到一双筷子，开始吃饭!", "饭吃完了，该开始思考问题了"}; 13. 14.DWORD WINAPI philosopher(LPVOID lParam); [...]]]></description>
			<content:encoded><![CDATA[<p>哲学家进餐问题描述：</p>
<p>有五个哲学家，他们的生活方式是交替地进行思考和进餐。哲学家们公用一张圆桌，周围放有五把椅子，每人坐一把。在圆桌上有五个碗和五根筷子，当一个哲学家思考时，他不与其他人交谈，饥饿时便试图取用其左、右最靠近他的筷子，但他可能一根都拿不到。只有在他拿到两根筷子时，方能进餐，进餐完后，放下筷子又继续思考。</p>
<p>01.#include<br />
02.#include<br />
03.<br />
04.#define P(S) WaitForSingleObject(S, INFINITE) //P操作<br />
05.#define V(S) ReleaseSemaphore(S, 1, NULL)<br />
06.#define NUMBERS 5 //叉子个数<br />
07.typedef HANDLE Semaphore; //信号量原型<br />
08.Semaphore chopstick[5];<br />
09.Semaphore mutex; //定义一个互斥量<br />
10.Semaphore room;<br />
11.char *philosophers[5] = {"哲学家1 ", "哲学家2 ", "哲学家3 ", "哲学家4 ", "哲学家5 "};<br />
12.char *str[] = {"得到左手边的筷子", "得到右手边的筷子", "得到一双筷子，开始吃饭!", "饭吃完了，该开始思考问题了"};<br />
13.<br />
14.DWORD WINAPI philosopher(LPVOID lParam); //线程函数声明<br />
15.void main()<br />
16.{<br />
17. HANDLE hThread[NUMBERS]; //线程句柄<br />
18. DWORD ThreadID[NUMBERS]; //线程ID<br />
19.<br />
20. //初始化信号量<br />
21. mutex = CreateSemaphore(NULL, 1, 1, "mutex");<br />
22. if(!mutex)<br />
23. cout&lt;&lt;"创建信号量失败"&lt;&lt;endl;<br />
24. for (int i=0; i&lt;5; i++)<br />
25. {<br />
26. chopstick[i] = CreateSemaphore(NULL, 1, 1, NULL);<br />
27. if(!chopstick[i])<br />
28. cout&lt;&lt;"创建信号量失败"&lt;&lt;endl;<br />
29. }<br />
30. for (i=0; i&lt;NUMBERS; i++)<br />
31. {<br />
32. hThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)philosopher, (LPVOID *)&amp;i, 0, &amp;ThreadID[i]);<br />
33. if (!hThread[i])<br />
34. {<br />
35. cout&lt;&lt;"创建线程失败"&lt;&lt;endl;<br />
36. }<br />
37. WaitForSingleObject(hThread[i], 5000);<br />
38. }<br />
39.}<br />
40.<br />
41.DWORD WINAPI philosopher(LPVOID lParam)<br />
42.{<br />
43. int i = *(int *)lParam;<br />
44. while (true)<br />
45. {<br />
46. P(mutex);<br />
47. P(chopstick[i]);<br />
48. cout&lt;&lt;philosophers[i]&lt;&lt;str[0]&lt;&lt;endl;<br />
49. P(chopstick[(i+1)%5]);<br />
50. cout&lt;&lt;philosophers[i]&lt;&lt;str[1]&lt;&lt;endl;<br />
51. cout&lt;&lt;philosophers[i]&lt;&lt;str[2]&lt;&lt;endl;<br />
52. V(chopstick[i]);<br />
53. V(chopstick[(i+1)%5]);<br />
54. cout&lt;&lt;philosophers[i]&lt;&lt;str[3]&lt;&lt;endl&lt;&lt;endl;<br />
55. Sleep(1000); //整个吃饭过程大约一秒时间<br />
56. V(mutex);<br />
57. }<br />
58. return 0;<br />
59.}</p>
]]></content:encoded>
			<wfw:commentRss>http://software.intel.com/zh-cn/blogs/2012/01/13/windows-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

