"Scalable Multithreaded Programming with Thread Pools" 阅读笔记

来源:互联网 发布:孤陋寡闻的网络 编辑:程序博客网 时间:2024/06/05 15:17

(MSDN Magzine 上的文章)

 

"There are many different ways to distribute your work across multiple cores. One of the easiest and most robust is called task-based programming."

"To achieve this state of multicore bliss, you’ll have to reexamine some of your preconceptions of how to attack a programming problem and rethink them in terms of task-based programming."

 

From Threads to Tasks

"

There are a few guidelines to keep in mind when converting a large single-threaded job into multithreaded tasks:

  • The large job can be arbitrarily divided into 1 to n tasks.
  • The tasks should be able to run in any order.
  • The tasks should be independent of each other.
  • The tasks must have associated context data.

 

Using the Thread Pool

"Threads require a lot of bookkeeping by the OS, so it’s not a good idea to wantonly create and destroy them. There are not-insignificant costs associated with creating and destroying a thread, so if you do this constantly, it’s easy to lose any advantage you gain by multithreading."

 

"The advantage of using a thread pool instead of creating your own threads is that the OS will take care of scheduling tasks for you—your job will be to keep feeding tasks into the thread pool so that the OS will be able to keep all of the hardware threads busy."

 

本来期待这篇文章能介绍 Thread Pool 的机制及调用建议, 结果往后面一看, 发现转到以快速排序为例子讲如何切分 task 去了, 没什么意思.

不过文章前面介绍基本原则的地方多少还是值得一看.

原创粉丝点击