公司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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库