Код: Выделить всё
asio::awaitable progress(auto strand) {
int i = 0;
for (;;) {
co_await dispatch(bind_executor(strand, asio::deferred));
ore::util::print("Strand Executor 1:", ++i);
// ...Do something with a synchronous resource...
co_await dispatch(asio::deferred);
ore::util::print("Back on IO Context Executor");
// ...Code that doesn't depend on the synchronous resource...
co_await dispatch(bind_executor(strand, asio::deferred));
ore::util::print("Strand Executor 2");
// ... Code that depends on the synchronous resource again...
}
}
int main() {
asio::io_context io;
auto my_strand = make_strand(io);
co_spawn(io, progress(my_strand), asio::detached);
io.run();
}
< /code>
Это имеет (по крайней мере) одну проблему, и мне любопытно об этом. Потому что я определил цепь как: < /p>
auto my_strand = make_strand(io);
< /code>
Это в конечном итоге оказатся после 2,6k итераций или около того. < /p>
...
T0 Strand Executor 1: 2657
T0 Back on IO Context Executor
Segmentation fault (core dumped)
Это работает, итерация работает нормально, если нити из разных потоков: < /p>
asio::thread_pool tp(1);
auto my_strand = make_strand(tp);
< /code>
... тестирование немного больше, я замечаю, что он проходит с еще более простым тестом: < /p>
asio::awaitable progress() {
for (;;) {
co_await dispatch(asio::deferred);
}
}
int main() {
asio::io_context io;
co_spawn(io, progress(), asio::detached);
io.run();
}
< /code>
Может быть, Segfault связан с реализацией Coaroutine? Я думал, что это должно быть без стека, но, возможно, это не так. ?
Примечание: код, измененный из этого сообщения в блоге
Подробнее здесь: https://stackoverflow.com/questions/793 ... ion-signal
Мобильная версия