by unchar1 on 6/6/24, 11:28 PM with 104 comments
by kelnos on 6/7/24, 5:17 AM
"Give me a random number" feels like it shouldn't require all this overhead and hidden complexity under the hood. But unless you've read a lot about implementation details, or have been bitten by that exact problem before, how would you know? You wouldn't, of course.
by loeg on 6/7/24, 12:18 AM
by teraflop on 6/7/24, 12:48 AM
(In practice, there is no way for a program to distinguish this from a single RNG that was initialized with a truly random seed that you can't recover, so it doesn't break backwards compatibility.)
Unfortunately, the C standard prescribes that failing to call `srand` must be equivalent to calling `srand(1)`, so I don't think glibc can get away with this trick.
Mentioned here: https://go.dev/blog/randv2
by kibwen on 6/7/24, 12:48 AM
by pclmulqdq on 6/7/24, 12:20 AM
by tombert on 6/7/24, 12:07 AM
by starik36 on 6/7/24, 2:01 AM
So, me, being brand new to OS/2, immediately wanted to try out the new multi-threading features. Without knowing anything about locks and spins or deadlocks or shared state, I plunged into it just using the OS/2 documentation and a programming book I bought. Keep in mind, this was a single CPU machine, probably an Intel 486dx or something like that.
I spent the next couple of weeks debugging all sorts of things that should not have been happening. There were crashes, lock up, slow downs, missing variables, etc... that I couldn't resolve.
After a while, I gave up and did what the OP did: just put everything in a loop and it solved everything.
by DeathArrow on 6/7/24, 5:20 AM
If the program was properly written there wouldn't be any issues with multithreading.
The title of the article gives the false impression that multithreading is slowing programs in the general case. In fact poorly written programs are slow and that is true for both multithreaded and singlethreaded programs.
If your program runs slow but you don't need it to run fast, don't bother.
If your program runs slow and you need it to run fast, go see what optimizations can be done.
by Animats on 6/7/24, 3:17 AM
Now try a multi-thread program compute bound program with more threads than CPUs that does Rust vector "push" operations. Push causes an array to grow, locks get set, other threads hit locks, threads go into spinlock mode, threads inside spinlocks context switch, lose control, and performance drops by two orders of magnitude. Most CPU time is going into spinlocks.
I'm not convinced that user-space spinlocks are a good idea. If you're never compute-bound, they can work, but if you run out of CPU time and a context switch occurs inside a spinlock, it's all downhill from there.
Windows itself doesn't do this; it has a different locking strategy.
by dang on 6/7/24, 2:24 AM
Make your program slower with threads - https://news.ycombinator.com/item?id=8711162 - Dec 2014 (47 comments)
by wonrax on 6/7/24, 2:38 AM
by overfl0w on 6/7/24, 9:18 AM
"The random() function should not be used in multithreaded programs where reproducible behavior is required. Use random_r(3) for that purpose."
Perhaps the notes should be updated to state that the performance of multi-threaded programs is also affected due to the shared global resource?
by beyonddream on 6/7/24, 2:10 AM
by InvOfSmallC on 6/9/24, 2:25 PM
by AndyKelley on 6/7/24, 2:49 AM
by oah on 6/8/24, 2:37 AM
However, the point of the article still stands.
by dkarl on 6/7/24, 2:42 AM
I remember in the early 2000s doubling the performance of a CPU-bound C++ program by using two threads, and my boss being shocked that I was able to do it. He had written it off as a naive idea. I was too young and inexperienced to understand why he was surprised; to me it just seemed like he didn't understand what I was doing. In retrospect, now I feel like maybe he had the right default expectation, and I got lucky that it worked out so easily.