Thread’s Benefit and Drawback
Benefit
- asynchronous event 像是使用者輸入、網路封包傳送和 disk I/O 在 single thread 時會陷入 blocking,而使用 multithread 就可以讓不同 thread 處理不同的 event
- 同個 process 間的 thread 自動共享 address space 及 file descriptors
- 再 multi-core processor 可以用 thread 達到平行處理
Drawback
- multithread 的 program 需要更謹慎的撰寫因為 thread 之間共享資源並且沒有保護很容易出現像是 race condition 之類的問題
- 很難 debug
- single processor 的機器用 multithread 可能反而會降速