While working on a thread (fiber) scheduling class, I found myself writing a function that never returns:
// New thread, called on an empty stack
// (implementation details, exception handling etc omitted)
[[noreturn]] void scheduler::thread() noexcept
{
current_task->state = running;
current_task->run();
current_task->state = finished;
while (true) yield();
// can't return, since the stack contains no return address.
}
This function is never directly called (by thread();
). It is "called" only by a jmp
from assembly code, right after switching to a new context, so there is no way for it to "return" anywhere. The call to yield()
, at the end, checks for state == finished
and removes this thread from the thread queue.
Would this be a valid use of the [[noreturn]]
attribute? And if so, would it help in any way?
edit: Not a duplicate. I understand what the attribute is normally used for. My question is, would it do anything in this specific case?