this post was submitted on 26 Mar 2025
521 points (96.9% liked)

Programmer Humor

22112 readers
964 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 142 points 5 days ago (3 children)

all programs are single threaded unless otherwise specified.

[–] [email protected] 45 points 5 days ago (3 children)

It’s safe to assume that any non-trivial program written in Go is multithreaded

[–] [email protected] 15 points 5 days ago (1 children)

And yet: You’ll still be limited to two simultaneous calls to your REST API because the default HTTP client was built in the dumbest way possible.

[–] [email protected] 1 points 3 days ago

Really? Huh, TIL. I guess I've just never run into a situation where that was the bottleneck.

[–] [email protected] 18 points 5 days ago (1 children)

But it's still not a guarantee

[–] [email protected] 1 points 3 days ago

Definitely not a guarantee, bad devs will still write bad code (and junior devs might want to let their seniors handle concurrency).

[–] [email protected] 6 points 4 days ago (1 children)

I absolutely love how easy multi threading and communication between threads is made in Go. Easily one of the biggest selling points.

[–] [email protected] 1 points 3 days ago (1 children)

Key point: they're not threads, at least not in the traditional sense. That makes a huge difference under the hood.

[–] [email protected] 1 points 2 days ago* (last edited 2 days ago) (1 children)

Well, they're userspace threads. That's still concurrency just like kernel threads.

Also, it still uses kernel threads, just not for every single goroutine.

[–] [email protected] 1 points 2 days ago (1 children)

What I mean is, from the perspective of performance they are very different. In a language like C where (p)threads are kernel threads, creating a new thread is only marginally less expensive than creating a new process (in Linux, not sure about Windows). In comparison creating a new 'user thread' in Go is exceedingly cheap. Creating 10s of thousands of goroutines is feasible. Creating 10s of thousands of threads is a problem.

Also, it still uses kernel threads, just not for every single goroutine.

This touches on the other major difference. There is zero connection between the number of goroutines a program spawns and the number of kernel threads it spawns. A program using kernel threads is relying on the kernel's scheduler which adds a lot of complexity and non-determinism. But a Go program uses the same number of kernel threads (assuming the same hardware and you don't mess with GOMAXPROCS) regardless of the number of goroutines it uses, and the goroutines are cooperatively scheduled by the runtime instead of preemptively scheduled by the kernel.

[–] [email protected] 1 points 2 days ago* (last edited 2 days ago)

Great details! I know the difference personally, but this is a really nice explanation for other readers.

About the last point though: I'm not sure Go always uses the maximum amount of kernel threads it is allowed to use. I read it spawns one on blocking syscalls, but I can't confirm that. I could imagine it would make sense for it to spawn them lazily and then keep around to lessen the overhead of creating it in case it's needed later again, but that is speculation.

Edit: I dove a bit deeper. It seems that nowadays it spawns as many kernel threads as CPU cores available plus additional ones for blocking syscalls. https://go.dev/doc/go1.5 https://docs.google.com/document/u/0/d/1At2Ls5_fhJQ59kDK2DFVhFu3g5mATSXqqV5QrxinasI/mobilebasic

[–] [email protected] 5 points 4 days ago

I initially read this as “all programmers are single-threaded” and thought to myself, “yeah, that tracks”

[–] [email protected] 23 points 5 days ago (2 children)

Does Python have the ability to specify loops that should be executed in parallel, as e.g. Matlab uses parfor instead of for?

[–] [email protected] 51 points 5 days ago (2 children)

python has way too many ways to do that. asyncio, future, thread, multiprocessing...

[–] WolfLink 40 points 5 days ago (1 children)

Of the ways you listed the only one that will actually take advantage of a multi core CPU is multiprocessing

[–] [email protected] 9 points 5 days ago (1 children)

yup, that's true. most meaningful tasks are io-bound so "parallel" basically qualifies as "whatever allows multiple threads of execution to keep going". if you're doing numbercrunching in pythen without a proper library like pandas, that can parallelize your calculations, you're doing it wrong.

[–] WolfLink 7 points 5 days ago* (last edited 5 days ago) (1 children)

I’ve used multiprocessing to squeeze more performance out of numpy and scipy. But yeah, resorting to multiprocessing is a sign that you should be dropping into something like Rust or a C variant.

[–] [email protected] 1 points 4 days ago

Most numpy array functions already utilize multiple cores, because they're optimized and written in C

[–] [email protected] 8 points 5 days ago (1 children)

I've always hated object oriented multi threading. Goroutines (green threads) are just the best way 90% of the time. If I need to control where threads go I'll write it in rust.

[–] [email protected] 7 points 5 days ago (2 children)

nothing about any of those libraries dictates an OO approach.

[–] [email protected] 2 points 5 days ago (1 children)
[–] [email protected] 1 points 5 days ago

Meh, even Java has decent FP paradigm support these days. Just because you can do everything in an OO way in Java doesn't mean you need to.

[–] [email protected] 0 points 5 days ago (1 children)

If I have to put a thread object in a variable and call a method on it to start it then it's OO multi threading. I don't want to know when the thread spawns, I don't want to know what code it's running, and I don't want to know when it's done. I just want shit to happen at the same time (90% of the time)

[–] [email protected] 4 points 5 days ago

the thread library is aping the posix thread interface with python semantics.

[–] [email protected] 7 points 5 days ago (2 children)

Are you still using matlab? Why? Seriously

[–] [email protected] 18 points 5 days ago (1 children)

No, I'm not at university anymore.

[–] [email protected] 4 points 5 days ago (1 children)
[–] [email protected] 5 points 5 days ago* (last edited 5 days ago) (1 children)

We weren't doing any ressource extensive computations with Matlab, mainly just for teaching FEM, as we've had an extensive collection of scripts for that purpose, and pre- and some post processing.

[–] [email protected] 1 points 5 days ago

I don't like that they don't write their own algorithms in any other language. I was trying to understand low-pass filters a while back and so many web pages were like, "Call this MATLAB function" or "here's a code generator that puts out bad C for specific filter parameters" Like no, I want the algorithm explained to me...

[–] [email protected] 7 points 5 days ago (1 children)

I was telling a colleague about how my department started using Rust for some parts of our projects lately. (normally Python was good enough for almost everything but we wanted to try it out)

They asked me why we're not using MATLAB. They were not joking. So, I can at least tell you their reasoning. It was their first programming language in university, it's safer and faster than Python, and it's quite challenging to use.

[–] [email protected] 4 points 5 days ago

"Just use MATLAB" - Someone with a kind heart who has never deployed anything to anything