News for hackers. Stuff that matters.
Re:No nuances, just buggy code (was: related to Spinlock imp... by sillysaurusx on Sunday January 05, 2020 @03:38AM (#21960334)
Why is calling sched_yield() a mistake? What's the correct way to yield CPU without calling sleep?
Re:No nuances, just buggy code (was: related to Spinlock imp... by mannykannot on Sunday January 05, 2020 @11:35AM (#21962032)
If a thread can do no more for the moment, then either it is finished, or it is waiting for something to happen. In the latter case, the best thing is to be woken on that event, and the next best is to poll for it. Sleep is only the best solution when that event is literally the passage of time.
Re:No nuances, just buggy code (was: related to Spinlock imp... by dante_dev on Sunday January 05, 2020 @04:01AM (#21960392)
you may want to read Linus's answer about it https://www.realworldtech.com/forum/?threadid=189711&curpost...
Re:No nuances, just buggy code (was: related to Spinlock imp... by atq2119 on Sunday January 05, 2020 @08:12AM (#21961106)
You really should just read the linked article, but the tl;dr is that sched_yield() does not convey intent (except in narrow realtime scenarios that you should not be using in the first place).

Tell the kernel why you want to yield CPU. Work with the kernel, not against it. Do you want to yield because you genuinely want to wait until some future point in time? Use sleep. Is it because you're waiting for some I/O? Use poll or its variants. Is it because you're waiting for a signal from another thread? Use mutexes, condition variables, etc. that call futex under the hood.

Re:No nuances, just buggy code (was: related to Spinlock imp... by gpderetta on Sunday January 05, 2020 @03:48AM (#21960361)
Futex wait, or any other blocking syscall.
Re:No nuances, just buggy code (was: related to Spinlock imp... by pizlonator on Sunday January 05, 2020 @02:49PM (#21963542)
sched_yield means “yield to any thread if it’s easy to do so or do nothing”. That meaning fits the requirements of some algorithms, particularly if you call it a finite number of times before doing something more drastic like a blocking syscall (like futex wait, lock acquisition, whatever) that will block until the condition you’re waiting for is fulfilled.

Think of it as a speculative optimization on top of busy waiting, not as a way to control the scheduler.

Re:No nuances, just buggy code (was: related to Spinlock imp... by colonwqbang on Sunday January 05, 2020 @05:40AM (#21960655)
It's explained in the linked post.

Why don't you want to call sleep?

Re:No nuances, just buggy code (was: related to Spinlock imp... by gpderetta on Sunday January 05, 2020 @07:41AM (#21961020)
Beacuse it would act as as an even worse sched yield.
Re:No nuances, just buggy code (was: related to Spinlock imp... by colonwqbang on Sunday January 05, 2020 @05:02PM (#21964467)
I guess I can't really see the use case here. If you want to sleep, it's usually because you're waiting for something: file descriptor, semaphore/mutex, or a certain amount of time to pass. If none of these conditions apply, can't you just continue to run and let the scheduler do its work? (maybe with an appropriate nice setting if you are doing "less important" work).

The scheduler itself uses fixed time interrupts for its basic functions, so "sleep(fixed_time_interval)" is what you will get in the end anyway on a preemptive system.

Re:No nuances, just buggy code (was: related to Spinlock imp... by mritun on Sunday January 05, 2020 @04:26PM (#21964230)
Calling sleep(1) instead of thread yield is faster - see the twitter thread for a proof.
Re:No nuances, just buggy code (was: related to Spinlock imp... by de_watcher on Monday January 06, 2020 @10:04AM (#21969619)
Still worse: it's a confusing code, and it's also confusing that it's faster.
Re:No nuances, just buggy code (was: related to Spinlock imp... by temac on Sunday January 05, 2020 @10:04AM (#21961505)
Not in every cases, but both are certainly suboptimal compared to a diretected wake up.