[coroutine] Resuming of coroutine from another thread

I'm thinking up a design for a task-processing system that uses Boost.Asio and Boost.Coroutine. As part of that design, I would have a pool of worker threads executing work items. During processing of a particular work item, it's possible that the worker might need to wait on some other operation (e.g. I/O) to complete. However, while it waits, I'd like to put that thread to work doing something else useful from the queue. I was thinking of using coroutines to provide the ability to suspend the processing of one task, restarting it later when it is ready to move forward again. In my hypothetical framework, the thread that finishes the work might not be the same one that started it in the first place. I recently came across this Stack Overflow question: http://stackoverflow.com/questions/8557067/thread-safe-coroutines-with-asio which inquired as to the safety of creating a coroutine in one thread and then resuming it in another. The answers there indicate that this is not supported, and they link to Boost.Coroutine documentation that explains the underlying problems and why it isn't supported. However, that documentation was from the original Boost.Coroutine by Giovanni Deretta (which I'm not sure was ever actually accepted into Boost). The latest version by Oliver Kowalke indicates that it is a follow-up on that library, but doesn't have any specific caveats in the documentation regarding the interaction between threads and coroutines. Does the contemporary Boost.Coroutine have the same limitation, or is it safe to create a coroutine in one thread and then resume it in another? Jason

On 9/14/2013 8:54 AM, Quoth Jason Roehm:
Does the contemporary Boost.Coroutine have the same limitation, or is it safe to create a coroutine in one thread and then resume it in another?
While I can't answer this question directly, I can point out that the examples for Boost.Asio 1.54 show using coroutines as composed operation handlers, and that naturally Boost.Asio makes no particular guarantees that only one thread will be used to run handlers (eg. if multiple threads have called "io_service::run"). So presumably it should be fine, unless this is an oversight somewhere. It's likely however that for things to remain safe you will have to either only have one "run" thread or to use strands or some other mutual-exclusion method to protect against concurrent execution of the same coroutine. (Also note that there have been some fairly dire warnings of problems with the IOCP scheduler in 1.54 on Windows, so tread carefully.)
participants (2)
-
Gavin Lambert
-
Jason Roehm