# 2023q1 Homework7 (ktcp) contributed by < [zeddyuu](https://github.com/zeddyuu/kecho) > ## Workqueue 我想在了解 CMWQ 前應該要先了解 Workqueue。 Workqueue 是 Linux kernel 的一種任務執行機制,顧名思義就是由任務所組成的佇列,當需要執行任務的時候會構造一個 work,塞進相應的 workqueue,再由 workqueue 所綁定的 worker (執行緒) 去執行 work。 使用者可以透過 [create_workqueue](https://elixir.bootlin.com/linux/v2.5.41/source/kernel/workqueue.c#L267) 對 workqueue 進行初始化,核心會分配與 CPU 個數相同的 cpu_workqueue_struct,並為每個 cpu_workqueue_struct 分配一個 kernel thread (worker thread)。 ## Concurrency Managed Workqueue (CMWQ) 在原來的 workqueue 實作中,在 MT wq(multi threaded workqueue) 每個 CPU 會有一個 worker thread,還有 ST wq(single threaded workqueue) 在整個系統只有一個 worker thread,隨著核心中 MT wq 的使用增多以及 CPU 核心增加,容易導致預設的 32K pid 被用完。 因為每個 wq 都會維護自己的 worker thread pool,MT wq 只能在每個 CPU 上同時執行一個工作,而 ST wq 只能在整個系統上執行一個工作,導致並行執行的程度較差。 於是就有了 CMWQ,基於 workqueue 的重新實作,並且注重以下的目標 >Maintain compatibility with the original workqueue API. Use per-CPU unified worker pools shared by all wq to provide flexible level of concurrency on demand without wasting a lot of resource. Automatically regulate worker pool and level of concurrency so that the API users don’t need to worry about such details. ## 引入 CMWQ