How to use coroutines properly?
I know, C++20 isn't out yet and coroutines are not meant for normal developers.
But I was using a library that I think would do quite well if it used ranges (and ranges are one of the things that is recommended to be implemented using coroutines) and coroutines (for lazy processing). Before proposing this idea, I wanted to get a few things clarified (for myself and for future discussions)
- is it sufficient for an object to define
begin()andend()to enable use of ranges on it? (Object has contiguous memory associated with begin and end) - is there a way to pass a coroutine (share it) from one function to another and continue the call (I saw it being alluded to in several posts)?
- Return the current iterator(?)
- Use it somehow with range?
- How does this interact with multiple heap allocations? Each instance is independent (thread safe) and any 2 calls into the same instance potentially cause data race aka the usual advice regarding data races in functors or different?
- is it possible to use something like placement new for the heap frame to use a pre-allocated buffer? Why/Why not? What about scenarios where there's no "dynamic memory" aka embedded?
- ^(This would help because it might be impossible for the compiler to elide the heap usage but it might be possible to have an upper limit on the stack memory usage)
- How does constexpr play with coroutines?
I'm currently reading the NewOldThing blog (Microsoft dev) articles and linked articles (latest link). I couldn't read the standardese about coroutines so I'm relying on blog posts.
I have decent experience in multi-threaded and async programming (not coroutines though) so any and all references/reading material/videos are welcome: novice or expert level, just it should be a not overly boring and (bit) concise resource.
Oh, and should I post this on r/cpp instead since it appears to not have code but discussion about the language feature?
Posted Dec 12, 2019, 2:46 AM UTC · edited