Home
Blog
Is Multithreading Worth It
Updated
Dot Net Perls

Is Multithreading Worth It

The program fails in an unexpected way, but only once every ten or twenty times it is run. This is a hard-to-debug problem: what causes the failure, and how can it be reproduced? In many programming languages, programs that use threads may have this sort of bug.

Most programming languages do not force developers to access all fields in a thread-safe way. So the burden falls on the developer to ensure the code is correct, and a hard-to-reproduce bug will not occur.

One language that enforces thread-safe access to fields is Rust:

Rust requires any field that is shared between threads to be stored in a Mutex, which means we cannot read or write it without having exclusive access to it (locking it).
This requirement means that a field cannot change while we are reading it on another thread.
The requirement for Mutex for mutable writes eliminates many multithreading bugs in Rust programs.

In languages like C# or Go it is possible to unsafely modify data while other threads are reading the fields. This can lead to complicated bugs where the program works correctly, but only 99% of the time. Without "fearless concurrency" as in Rust, I feel multithreading should be largely avoided—it is more trouble than it's worth in most cases.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
An RSS feed is available for this blog.
Home
Changes
© 2007-2025 Sam Allen