虽然上线(光纤)调度类的工作,我发现自己写一个永远不会返回的函数:这是noreturn属性的有效用法吗?
// 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.
}
此功能永远不会直接调用(由thread();
)。它仅在汇编代码中被“调用”jmp
,在切换到新的上下文之后,因此无法在任何地方“返回”。最后,调用yield()
检查state == finished
并从线程队列中删除此线程。
这是[[noreturn]]
属性的有效用法吗?如果是这样,它会以任何方式帮助吗?
编辑:不重复。我明白这个属性通常用于什么。我的问题是,它会在这个特定情况下做什么?
_它被“jmp”调用......这听起来不对。 – erip
嗯,虽然我确定会发生一些有趣的黑客行为,但这听起来不像我会让我的项目依赖的图书馆。避免不完全依赖标准C++的第三方库并不是最坏的想法。 –
可能重复的[noreturn的点是什么?](http://stackoverflow.com/questions/10538291/what-is-the-point-of-noreturn) –