[問題] 應用Semaphore於容器

看板C_and_CPP作者 (_00_)時間4年前 (2020/05/11 18:55), 4年前編輯推噓0(004)
留言4則, 1人參與, 4年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) OSX / Xcode 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) clang 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): Hi大家好 先附上github https://github.com/clydewu/Cldlib/tree/master/Cldlib 因為之前是以C++11標準,還沒有Semaphore 所以用condition variable實作了Semaphore 然後使用這個semaphore來給予queue最大最小值的限制 若queue.size()超過max或小於min,那嘗試push或pop的的thread就會等待 實作的方式是一個樣板類別,裡面有deque跟semaphore template <typename T> class SemaphoreQueue { deque<T> queue_; Semaphore* in_semaphore_; Semaphore* out_semaphore_; } 那現在希望同樣的邏輯可以reuse在List或其他容器上 有點不知道怎麼做 初步的想法是要有兩個樣板參數 一個是容器本身的類型 一個是容器裝的東西的類型 e.g. template <typename T1, typename T2> class SemaphoreContainer { T1<T2> queue_; Semaphore* in_semaphore_; Semaphore* out_semaphore_; } 但這樣根本編不過XD 不知道這是要用什麼c++的機制來做呢? 基礎不紮實,關鍵字請求摟! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.249.37 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1589194552.A.0EB.html ※ 編輯: forgmonkey (125.227.249.37 臺灣), 05/11/2020 19:00:10

05/11 20:00, 4年前 , 1F
你的template只需要container type就可以了,在用containe
05/11 20:00, 1F

05/11 20:00, 4年前 , 2F
r::value_type去取內容物的type
05/11 20:00, 2F

05/11 20:01, 4年前 , 3F
想弄漂亮一點就多加一些compiler assert給點錯誤訊息,然
05/11 20:01, 3F

05/11 20:01, 4年前 , 4F
後註解寫清楚傳入的container要支援哪些api
05/11 20:01, 4F
喔喔,是說使用的時候是像 SemaphoreContainer<deque<string>> MyQueue;這樣嗎? 喔喔喔,感謝! ※ 編輯: forgmonkey (122.116.126.210 臺灣), 05/12/2020 00:14:57
文章代碼(AID): #1UkIyu3h (C_and_CPP)