Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Can you clarify what you mean? You seem to imply that using multiple threads is not a good idea, but then also express that not using multiple threads is also not a good idea. What's left?


sort by: page size:

Nice point. I'll add it to the list of reasons to avoid multi-threading things wherever possible!

So how do you implement multithreading if the above are a bad idea?

I think you're being downvoted because people disagree with what you're saying. In my experience you need multiple threads when you want multiple things to happen at the same time. i.e. if I have a client/server architecture and one client instructs the server to perform a long running task then I don't want the server to appear frozen to all my other clients, which it would if the server ran in a single thread. I don't really see how you can get around this. Do you have a solution?

The point I am ultimately trying to make is that, in many cases, the reason to avoid multi-threading is because it's not actually the fastest way to do something. It's that simple.

The implicit concern (if you could not infer from the content of my link above) being that the developer community is completely blind to the performance capabilities of a single computer/core/thread in 2022 and are inclined to automatically skip over these simple solutions in favor of a multi-threaded solution.


My opinion is the opposite, to the point I would argue that anyone advocating for multithreading for reasons other than executing things in parallel on different cores is extremely dangerous and shouldn't be allowed anywhere near a serious codebase.

That's not a good enough reason not to write multithreaded software. You can apply your reasoning to almost any software, and it will fail all the same. Performance and latency matter. A lot.

OK, but surely there are cases where it would be useful (I would argue necessary) to have a single process with multiple threads - when dealing with GUIs for example.

How about: Use threads, but only in scenarios when it's simpler or when you need to execute on multiple cores on parallel.

Basic rules for multithreading:

1) Don't. You don't need to.

2) Still don't.

3) If you really, really do need threads, keep it as simple as possible and make sure that every access that needs to be synchronized, is synchronized.

I've been programming for a long time now and I've seen precious few cases where multithreading is legitimately a better option, let alone the only option. Usually even then it's just about using more cores.


I agree strongly with this. Multithreaded code is super hard to understand and super error-prone when you're dealing with threads and locks yourself.

In development I don't need it to be multi-threaded. 1 thread is fine, as long as I can explain, step-by-step, how the calculations produced the output.

> In web-apps architectures, you have a choice: multithread or multiprocess.

No, the choice is definitely not that limited. Saying something like this is like saying there's no Nginx, only Apache available as an HTTP server. For the concurrent, IO-bound code you can leverage all kinds of concurrency approaches (coroutines, green threads, callbacks). In these use cases, multiple threads are actually a bad idea from the memory efficiency perspective.

On the other hand, multiple threads would be viable for CPU-bound and/or long-running code were it not for the GIL, that's true. With the GIL you don't have that option and have to resort to multiprocessing.

Multiprocessing is not so bad, actually, although it does make the code more complicated. Unless your problem is massively parallel (but then you'd use GPU instead), spawning n x 2 processes for n cores is definitely possible with how much RAM is available nowadays on the servers. You get optimal parallelism at the cost of serialization overhead (or other complexities if you want to directly share memory).

There are some languages which do support most existing concurrency mechanisms and they may be a better fit for a particular project. However, not supporting parallelism via multi-threading hardly disqualifies any language, provided the other tools are in place, solid and widely used.


Whenever I see someone arguing their code is better because it's multithreaded, I cringe.

Most developers cannot do multithreading correctly, and unless you're particularly good about it it's just going to introduce not only lots of bugs but also performance problems.

The only folks in that space that seem to do it well are ScyllaDB.


What about multiple processes? Threads and multiple processes are not the same, and using multiple processes cuts out a lot of the issues with threads while maintaining the concurrency.

Quite. I'm so fed up of the "threads are bad" argument (in my mind it's been commonplace since about 2008, so it's interesting to see this piece from 1995).

I've made use of threads at some point in almost every single job of any duration. They're one of many problem solving tools and if you understand them, which isn't particularly difficult, at some point you're bound to run into a problem that's a natural fit for a multi-threaded solution.

Nowadays, especially with no shared state, they're super-easy to use on many platforms. Take, for example, the parallel support in the .NET framework, along with functionality that supports debugging multi-threaded apps in Visual Studio like the ability to freeze threads.

If you do need to share state, which is when locking becomes essential, most languages and platforms have easy to use constructs to help you do this without much in the way of drama.

I'm not suggesting for a minute that there are no dangers, but there are plenty of dangers with other programming techniques, as well as lurking in any system of sufficient complexity, so I don't really understand why threads garner so much hate.


It's a decision choice to be single-threaded. Using threads requires locking or sophisticated concurrent data structures which can sometimes outweigh benefits in both code correctness/maintainability and performance.

Keep in mind: Don't use multiple threads unless you really, really need to, and have thought long and hard about concurrency issues.

In a way, I think the fact that many library functions are not thread-safe should be viewed as an encouragement to not use threads, or use them only for the bare minimum necessary.

I say this from a few decades of experience fighting with race conditions and the like, and whereupon several times I rewrote an existing multithreaded process into a single-threaded one and greatly improved performance and reduced memory usage. The architecture astronauts may have moved on to stuff like microservices now, but in the 90s/2000s threads were overused just as much.


Multithreading, why does everyone always do it wrong?

One of life's big questions.


Agreed, threads should be used only when true CPU concurrency is needed, and it is Too hard for most programmers to use (correctly). See John Ousterhout's presentation: http://www.cs.ubc.ca/~norm/508/2009W1/ouster95threadsbad.pdf
next

Legal | privacy