Hacker Newsnew | past | comments | ask | show | jobs | submit | AnanasAttack's commentslogin

Correctly using a spinlock: Never when preemption is enabled


Therefore, also never when hold times and/or contention are non-trivial (i.e. obviating the PAUSE addition).


That one isn't actually as strict. Just because you used a spinlock doesn't mean you cause pathological degradation.

spinlocks in systems with preemption are only allowed if the scheduler knows about them, though. Otherwise a throughput-optimizing scheduler may cause hold times in the range of seconds, by keeping an aquiring thead active with the holding thread asleep. Such a scheduler wouldn't be suitable for interactive workloads, but for non-networked batch tasks it should be quite efficient (due to a combination of calling the scheduling logic less often (thus wasting less time on it), and less cache contention with the application.

In interrupt handlers and such you may need to use spin locks, as you can't sleep either way. The rule about preemption still applies though, and aside from fairness issues, will take care of preventing pathological hold times.


Yes that's right!


You can totally have memory barriers in macros. It doesn't have much to do with the language at all


I always feared sites would do things like this so I clear the clipboard/fill it with garbage after usage


People do whatever they can to "win" on these benchmarks, so the resulting code is often not idiomatic


The recommendation is to write only idiomatic code that implements a certain algorithm without tricks: https://salsa.debian.org/benchmarksgame-team/benchmarksgame/...

Did you actually see programs that do not follow the guidelines or you're just stating this as what you would expect?


https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

Templating Lua here is probably borderline really.


Seems like those Lua fasta programs should be removed.


What someone else considers to be "idiomatic", you might not consider to be "idiomatic".


It seems anything is newsworthy here when it's in Rust


The comment doesn't explain why there even is an offset in the first place. Who put it there and why? I think this should be explained as well


C++ coroutines are stackless


Thread scheduling (or waking up cores) is slow. Because of this, mutexes will look better on dumb benchmarks, as the contending threads keep going to sleep, while the single succesful owner has practically uncontended access


There are various degrees of slow, in addition to kernel being smarter about multiple cores and SMT siblings than your application.

Kernel can run your code on a cooled core, giving it higher clock, for example. Ultimately making it run faster.

Of course this won't show in a benchmark where all the threads do mostly calculation rather than contention, but that's not the typical case. That mostly shows up in compute such as multithreaded video where latency does not matter one bit.

Typically you have more of a producer/consumer pattern where consumer sleeps, and it's beneficial to run it on a cold CPU, assuming the kernel woke it up beforehand.

Source: hit some latency issues with an ancient kernel on a nastily hacked big little architecture ARM machine. It liked to overheat cores and put heavy tasks on the overheated ones for alleged power saving. (Whereas running a task quicker saves power.)


I can't be the only one who finds it cleaner to just use structs with state and pointers to them, than all such macro and typedef hackery


Structs with state and pointers to them? Might as well just use C with classes.


Only stød is unusual. It is written with a superscript glottal stop (ˀ) letter


:-)


Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: