Почему он сбои при вызове coroutine_handle.destroy при использовании Coroutines в C ++ 20?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Почему он сбои при вызове coroutine_handle.destroy при использовании Coroutines в C ++ 20?

Сообщение Anonymous »

Я использую C ++ 20 с MSVC 2022 и внедрил простую структуру коратики. Тем не менее, я сталкиваюсь с сбоем при вызове coroutine_handle.destroy (). Вот минимальный воспроизводимый пример моего кода: < /p>
#include
#include
#include
#include
#include

struct SimpleCoroutinePromise;

struct SimpleCoroutine {
using promise_type = SimpleCoroutinePromise;
std::coroutine_handle handle;

SimpleCoroutine(std::coroutine_handle handle) : handle(handle) {}

SimpleCoroutine(const SimpleCoroutine&) = delete;
SimpleCoroutine& operator=(const SimpleCoroutine&) = delete;

SimpleCoroutine(SimpleCoroutine&& other) noexcept : handle(other.handle) {
other.handle = nullptr;
}

SimpleCoroutine& operator=(SimpleCoroutine&& other) noexcept {
if (this != &other) {
if (handle) {
handle.destroy(); // Destroy the current handle if it exists
}
handle = other.handle;
other.handle = nullptr;
}
return *this;
}

void resume() {
if (handle) {
handle.resume();
}
}

~SimpleCoroutine() {
if (handle) {
handle.destroy();
}
}
};

struct SimpleCoroutinePromise {
SimpleCoroutine get_return_object() {
SPDLOG_INFO("get_return_object");
return SimpleCoroutine(std::coroutine_handle::from_promise(*this));
}

void return_void() {
SPDLOG_INFO("return_void");
}

std::suspend_always initial_suspend() noexcept {
SPDLOG_INFO("initial_suspend");
return {};
}

std::suspend_never final_suspend() noexcept {
SPDLOG_INFO("final_suspend");
return {};
}

void unhandled_exception() {
SPDLOG_INFO("unhandled_exception");
}
};

SimpleCoroutine MySimpleCoroutine() {
SPDLOG_INFO("Coroutine Start");
co_return; // This will directly return, and the coroutine ends here
SPDLOG_INFO("Coroutine End"); // This line will not be executed
}

int testSimpleCorontine() {
SPDLOG_INFO("Main thread started executing 1");
auto coro = MySimpleCoroutine();
SPDLOG_INFO("Main thread started executing 2");
coro.resume();
return 0;
}

int main(int argc, char **argv){
SPDLOG_INFO("Hello, World!");
testSimpleCorontine();
return 0;
}
< /code>
Сбой возникает при разрушении, в частности, во время вызова обработки.destroy (). Я подозреваю, что это может быть связано с жизненным циклом Coroutine или тем, как управляется ручка.[2025-07-23 21:49:03.846] [info] [main.cpp:5] Hello, World!
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:78] Main thread started executing 1
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:48] get_return_object
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:57] initial_suspend
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:80] Main thread started executing 2
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:72] Coroutine Start
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:53] return_void
[2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:62] final_suspend


Подробнее здесь: https://stackoverflow.com/questions/797 ... routines-i
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»