C_Sharp 板


LINE

各位先進打擾了: 最近在學習 C# 的 thread 使用方法, 目前是參考 msdn 上的 producer / consumer 範例來修改 http://msdn.microsoft.com/zh-tw/library/yy12yx1f(v=VS.80).aspx 以下是我修改後的程式碼: ========================================================= using System; using System.Threading; using System.Collections; using System.Collections.Generic; public class SyncEvents { public SyncEvents() { _newItemEvent = new AutoResetEvent(false); _exitThreadEvent = new ManualResetEvent(false); _eventArray = new WaitHandle[2]; _eventArray[0] = _newItemEvent; _eventArray[1] = _exitThreadEvent; } public EventWaitHandle ExitThreadEvent { get { return _exitThreadEvent; } } public EventWaitHandle NewItemEvent { get { return _newItemEvent; } } public WaitHandle[] EventArray { get { return _eventArray; } } private EventWaitHandle _newItemEvent; private EventWaitHandle _exitThreadEvent; private WaitHandle[] _eventArray; } public class Producer { public Producer(ref Queue<int> q, ref SyncEvents e) { _queue = q; _syncEvents = e; } public void ThreadRun() { int count = 0; Random r = new Random(); while (!_syncEvents.ExitThreadEvent.WaitOne(0, false)) //while ((WaitHandle.WaitAny(_syncEvents.EventArray)) != 1)//queue為空 { lock (((ICollection)_queue).SyncRoot) { count++; _queue.Enqueue(count); _syncEvents.NewItemEvent.Set(); Thread.Sleep(10); } } Console.WriteLine("Producer thread: produced {0} items", count); } private Queue<int> _queue; private SyncEvents _syncEvents; } public class Consumer { public Consumer(ref Queue<int> q, ref SyncEvents e) { _queue = q; _syncEvents = e; } public void ThreadRun() { int count = 0; while ( (WaitHandle.WaitAny(_syncEvents.EventArray)) != 1) //while (!_syncEvents.ExitThreadEvent.WaitOne(0, false)) //會當掉 { lock (((ICollection)_queue).SyncRoot) { int item = _queue.Dequeue(); Thread.Sleep(10); } count++; } Console.WriteLine("Consumer Thread: consumed {0} items", count); } private Queue<int> _queue; private SyncEvents _syncEvents; } public class ThreadSyncSample { private static void ShowQueueContents(ref Queue<int> q) { // 這裡為何只有 look ??? lock (((ICollection)q).SyncRoot) { Console.WriteLine("{0} ", q.Count); } } static void Main() { // 設定二個要給 thread 用的共用資料結構 Queue<int> queue = new Queue<int>(); SyncEvents syncEvents = new SyncEvents(); // 建構 producer/consumer Console.WriteLine("Configuring worker threads..."); Producer producer = new Producer(ref queue, ref syncEvents); Consumer consumer = new Consumer(ref queue, ref syncEvents); // 令 producer/consumer 的ThreadRun 可在 thread 中運行 Thread producerThread = new Thread(producer.ThreadRun); Thread consumerThread = new Thread(consumer.ThreadRun); // 令 二個 thread 中啟動 Console.WriteLine("Launching producer and consumer threads..."); producerThread.Start(); consumerThread.Start(); // 顯示 queue 中的內容 for (int i = 0; i < 4; i++) { Thread.Sleep(2500); ShowQueueContents(ref queue); } // 下指令讓 thread 可以離開 Console.WriteLine("Signaling threads to terminate..."); syncEvents.ExitThreadEvent.Set(); // 等待 thread 關閉 producerThread.Join(); consumerThread.Join(); } } ======================================================== 基本上我是希望 producer 產生一個數值放到 queue 並送出一個訊號後, 然後 consumer 則會從 queue 中消除剛才產生的數值, 之後,producer 則會因為收到訊號而再產生一個新數值, 讓 queue 的內容不是放1個就是放0個, 但是這樣改的結果卻是 queue 所存的個數一直在上升中, 是誰發訊號給 producer 呢?不是只有當 consumer 處理完才由 consumer 送出的嗎? 另一個問題則是 while 的功能應該是在判定是否有發出 _exitThreadEvent 嗎? 為何互換後會產生問題呢? 謝謝 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.23.55







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP