雖然上線(光纖)調度類的工作,我發現自己寫一個永遠不會返回的函數:這是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) –