gpt4 book ai didi

c++ - 包裹在协程中的 boost asio 计时器导致 clang-5.0 上的 SEGFAULT

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:12 25 4
gpt4 key购买 nike

以下代码在启用协程支持的 clang-5.0 上导致 SEGFAULT。您可以在此处在线运行代码: wandbox compiled code

我正在使用编译器选项:

-stdlib=libc++ -fcoroutines-ts

当我在 GDB 下运行它时,它会在 coro.resume() 之后在 async_await 中出现 SEGFAULT;叫做。永远不会达到 await_resume 功能。我希望这是一个对象生命周期问题。几乎相同的一段代码在 MSVC 2017 下编译和运行没有任何问题。

程序的输出是:

i=0
Segmentation fault
Finish

源代码:

#define BOOST_THREAD_PROVIDES_FUTURE 
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION // Enables future::then
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/asio/system_timer.hpp>

#include <experimental/coroutine>
#include <chrono>
#include <iostream>

namespace std
{
namespace experimental
{
template <typename... Args>
struct coroutine_traits<boost::future<void>, Args...> {
struct promise_type {
boost::promise<void> p;
auto get_return_object() { return p.get_future(); }
std::experimental::suspend_never initial_suspend() { return {}; }
std::experimental::suspend_never final_suspend() { return {}; }
void unhandled_exception(){}

void return_void() { p.set_value(); }
};
};
}
}

namespace asioadapt
{
using namespace std::experimental;

template <typename R> auto operator co_await(boost::future<R> &&f) {
struct Awaiter {
boost::future<R> &&input;
boost::future<R> output;
bool await_ready() { return false; }
auto await_resume() { return output.get(); }
void await_suspend(std::experimental::coroutine_handle<> coro) {
input.then([this, coro](auto result_future) mutable{
this->output = std::move(result_future);
coro.resume();
});
}
};
return Awaiter{ static_cast<boost::future<R>&&>(f) };
}

template <typename R, typename P>
auto async_await(boost::asio::system_timer &t, std::chrono::duration<R, P> d) {
struct Awaiter
{
boost::asio::system_timer &t;
std::chrono::duration<R, P> d;
boost::system::error_code ec;

bool await_ready() { return false; }
void await_suspend(std::experimental::coroutine_handle<> coro) {
t.expires_from_now(d);
t.async_wait([this, &coro](auto ec) mutable {
this->ec = ec;
coro.resume();
});
}

void await_resume() {
if (ec)
throw boost::system::system_error(ec);
}
};

return Awaiter{ t, d };
}
}

using namespace boost;
using namespace boost::asio;
using namespace std::chrono_literals;
using namespace asioadapt;

boost::future<void> sleepy(io_service &io, int &i) {
system_timer timer(io);
std::cout << "i=" << i << std::endl;

co_await async_await(timer, 100ms);
++i;
std::cout << "i=" << i << std::endl;

co_await async_await(timer, 100ms);
++i;
std::cout << "i=" << i << std::endl;

co_await async_await(timer, 100ms);
++i;
std::cout << "i=" << i << std::endl;
}


void test(){
int i = 0;
io_service io;
auto future = sleepy(io, i);
io.run();

std::cout << "i=" << i << std::endl;
}


int main()
{
test();
}

最佳答案

我忽略了 lambda 捕获 await_suspend 函数中的 &。所以 coroutine_handle 在这次调用后被销毁。

关于c++ - 包裹在协程中的 boost asio 计时器导致 clang-5.0 上的 SEGFAULT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47407499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com