Hacker News new | past | comments | ask | show | jobs | submit login

I wouldn’t be surprised if the C++ coroutine push was for a perf. packet in some big tech company. Promo is hard.

Meanwhile, the same optimizations one could use for efficient coroutines could just as well be applied to fibers, which are much nicer.




The semantics of fibers are difficult to define precisely. For example using thread local storage inside fibers can be problematic if a fiber sleeps in one thread and restarts in another, because the compiler is free to store something like &errno and use it across function calls. (And this is not theoretical—compilers will do this).


You see this exact problem in task based libraries like task based parallel libraries such as taskflow. It's easy to get burned using thread local variables if you are not careful because a task might yield to the scheduler which runs another task ( of the same type ) on the same thread and then your thread local is corrupted.

You end up requiring more sophisticated object pools where you can check out and check in objects.


What bonzini is referring to is actually a more subtle issue: the compiler will CSE the (hidden) thread_local address calculation even across function calls. So even if you are careful and do not assume that thread_local state is preserved across function calls, your code can still be wrong as suddenly it will be accessing a thread_local owned by another thread. That might be as simple as dereferencing the wrong errno.

It is very hard to workaround that in your code. The only practical solution is to never migrate coroutines to other threads.


Well, it would be nice if, optionally, GCC didn't do that. I believe that MSVC has a flag to prevent exactly this optimization.


Stackless coroutines are useful in domains where stackful coroutines are simply not an option. Not every application runs on a server. There are tradeoffs associates with stackless coroutines, but I don't think it's questionable whether it was the right choice for a language like c++. If you want stackful coroutines, there are likely better suited languages for your domain that are not c++.


They were initally proposed by Microsoft, based on the way C# coroutines work on top of async/await, and the asynchrounous runtime used in .NET Native and C++/CX for WinRT.

Search for talks from Gor Nishanov.




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

Search: