公司effective concurrency培训笔记 by Herb Sutter
三天的培训,第一天去发现讲师是个c++牛人:Herb Sutter,公司花了一个人2000多刀,感觉不是很值,废话少说,记点笔记先,也算是学过,以后用到也好reference下:
题目是《Effecitve Concurrency Introduction and Overview》
1. Three Pillars of Dawn
- Concurrency For Isolation : Responsiveness
- Parallelism For Scalability : Throughput, scalability
- Consistency By Synchronization : Race-fess, deadlock-free
2. Concurrency Fundamentals:
- A Few Good Primitives : Threads, pools, lambdas, futures, locks and atomics
- Think In Transactions : Exception safety, public functions, and locked blocks
- Habitually Prefer Structured/Bounded Lifetimes : Locks, tasks, and more
- Recognize and Avoid Today's Overheads : Switching, contention, the const of unrealized parallelism
- Understand the Ture Nature of Deadlock : locks + messages + I/O + any other blocking
- concurrency在server端好解决,在client端难解决。
- 未来的芯片趋势是Mixed large & small cores的多核系统。
3. Active Object Pattern
- 使用Active Object来封装所有需要异步或者需要自己管理threads的地方
- Active Object可以用在GUI线程,decouple independent works,封装resources(I/O, shared objects)
- 需要实现一个Active Helper
- 当需要在Active Object返回值时可以使用“Out” parameters or send a message back
4. Machine Architecture, Performance and Scalability
- root of all hardware complexity : Latency (correctness + performance)
- Latency和Bandwidth : Bandwidth 可以用钱买,Latency有Limit
- 目前的瓶颈在于memory而不在CPU : int i = i1 + i2; double d = d1 * d2 需要的cost其实是一样的,大概都是15的cpu circles.因为14个circles都花在等momory上了
- Latency从少到多分别是Registers, L1 Cache, L2 Cache, Locak DRAM, Remote DRAM, Flash, Disk
5. How to Migrate Code to the Many Core “Free Lunch”
- Natural Parallelism : Quicksort, trees, graphs, hash tables
- 好处 :在多核上更快,充分利用多核
- Pay :Races, deadlocks, cost vs sequential
- 基本的并行逻辑 :Fork->Run->Join
- 了解 scalability :不是核越多越快,需要考虑很多别的东西(memory, I/O)
- 选择合适的data structures : 需要同时考虑Concurrency和Parallelization
6. How to stop threads
a. Kill : Thread.Abort (Avoid, almost sertain to corrupt transactions)
b. Tell and don't take no for an answer : pthread_cancel (Rudem only for languages without exceptions/unwind)
c. Ask : Thead.Interrupt (Better, but hard to use in practice)
d. Flag and let it poll (Best, though requres cooperative effort)
7. Consistency : Saft Locking
- Prefer scoped locking and always have objects own locks
- Avoid Races : Associated data with locks
- Avoid Deadlock : Apply lock hierarchies and other lock ordering
- Acoid Composability Problems : Don't call unknow code while holding a lock