Re: [問題] Thread Pool/Queue的設計方式

看板C_and_CPP作者 (littleboypis)時間15年前 (2009/03/24 23:57), 編輯推噓1(104)
留言5則, 2人參與, 最新討論串3/4 (看更多)
: 我以前用過或自己做的, 通常都是用 producer consumer queue. : producer-consumer queue 詳細的做法去 google 一下就 : 找得到了. 大概而言是靠兩個 monitor/condition, 一個是代表 : queue有 entries 一個代表 queue not full. 當 queue empty 的話, consumer : (在你的情況, 是 pool 裡的 thread) 就 wait on queue有 entries : 的 condition, 直到有人放東西進 queue 就 signal該 condition, : 喚醒其中一個 thread 來讀東西工作. (queue not full 則是當 : queue 的上限到達後, 讓 producer wait 直到有 consumer dequeu) : 大概 Thread Pool 的效能分別不會大到哪裡. 選自己合用的比較重要吧. : 試試看看 Glib 合不合用? 先謝謝你 的回應 C_and_C++的推文 推 visor:mutex~~~~~~~~~~ 220.135.160.66 03/24 12:10 → adrianshum:用 mutex 根本無補於事... 202.155.236.82 03/24 14:18 → visor:配上 condition 就有用啦 220.135.160.66 03/24 15:20 → adrianshum:那就是用 condition, 不是用 mutex了啦 202.155.236.82 03/24 17:34 → adrianshum:其實我在 C/C++ 版的答案就有提過了 202.155.236.82 03/24 17:35 我有上網去找了相關的資料了 但是想問一個問題 Thread Pool 是只需要用到pthread_cond_wait就OK了嗎? 需要搭配mutex嗎? 因為我看到一個例子 雖然他不是Thread Pool 而是一個 producer consumer program 但是他有用pthread_cond_wait 和 pthread_mutex_lock URL: http://bbs.yoyo-do.com/thread-85443-1-2.html 所以想問一下 是不是可以只用pthread_cond_wait來完成一個Thread Pool 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.105.238

03/25 10:08, , 1F
應該沒辦法吧?看到的範例都是配合mutex的 如果不要用這個
03/25 10:08, 1F

03/25 10:09, , 2F
改用Semaphore看看?
03/25 10:09, 2F

03/25 12:17, , 3F
condition 不能容許多個人同時 acquire, 所以就算用
03/25 12:17, 3F

03/25 12:17, , 4F
semaphore, 也只能設 1 個 resource count, 那麼和
03/25 12:17, 4F

03/25 12:17, , 5F
mutex 無異了
03/25 12:17, 5F
文章代碼(AID): #19oGBF-7 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19oGBF-7 (C_and_CPP)