摆脱你的(互)排,你值得更好。
Ditch your (mut)ex, you deserve better

原始链接: https://chrispenner.ca/posts/mutexes

## 并行编程的陷阱与超越互斥锁 现代计算机拥有多个CPU核心,使得并行编程至关重要。然而,尽管经过数十年的研究,有效管理并发仍然出乎意料地困难。像互斥锁这样的传统工具,虽然在简单情况下有效,但并未显著发展,并且随着系统规模的增长,会引入显著的复杂性。 核心问题在于共享可变状态,导致数据竞争和不一致的系统状态。试图用互斥锁修复这些问题,常常会导致封装失效、代码重复,以及令人头疼的死锁。作者通过银行账户转账的例子,展示了这些问题,强调了看似正确的代码在并发情况下可能失效。 幸运的是,存在替代方案。作者提倡使用不可变性作为主要防御手段,从而完全避免数据竞争。当*确实*需要可变状态时,**软件事务内存 (STM)** 提供了一种更安全、更可组合的解决方案。STM将操作视为原子事务,保证一致性并简化并发管理,避免了许多与互斥锁相关的陷阱。 虽然Actor模型和CSP对于任务并行性很有价值,但STM在跨多个工作者同步数据时表现更出色。最终,作者敦促开发者仔细权衡互斥锁的优缺点,并探索更强大的并发工具,以构建可靠、可扩展的系统。

相关文章

原文

Having access to multiple parallel CPU cores isn't a new thing by any means, people have been programming in parallel for half a century now, but recent years we've found ourselves at an inflection point. Moore's law is dying, beefy single cores are no longer keeping up. Modern computers come with multiple CPU cores, so exploiting parallel compute is more important than ever. Given how long it's been an area of research we can naturally expect that effective tools have taken root and that synchronizing threads is trivial now right...?

Unfortunately this has not been my experience, and I'm willing to bet it hasn't been yours either. Managing shared state across threads is hard, and the most commonly used tools: mutexes and semaphores, simply haven't evolved much since their inception.

The words that follow will dig into the problems inherent to mutexes and synchronizing shared mutable state. Afterwards we'll look into other avenues which should prove more helpful.

The Problem with Shared State

Let's begin by crafting a simple software system which needs synchronization in the first place.

I'll present a commonly used example: the task of managing bank account balances correctly in spite of parallel transfer requests.

Of course real banks don't store all their account balances in RAM, so I'll hope that the reader can apply the concepts from this pedagogical example to a their own domain as necessary, it serves as a stand-in for any sufficiently complex system which requires ad-hoc synchronization of arbitrary data between multiple threads.

Here's some golang'ish pseudo-code (please don't try to actually compile it) for a simple bank account and the operations upon it. I'm focused on the synchronization problems here, so forgive me for skipping the double-entry accounting, input validation, and other real-world complexities.

on a matter of principle.

So what can we do? I suppose we'll need to pull the locks out of withdraw and deposit so we can lock them in transfer instead.

my Patreon to keep up with my projects, or check out my book: It teaches the principles of using optics in Haskell and other functional programming languages and takes you all the way from an beginner to wizard in all types of optics! You can get it here. Every sale helps me justify more time writing blog posts like this one and helps me to continue writing educational functional programming content. Cheers!

联系我们 contact @ memedata.com