Я использую 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
Почему он сбои при вызове coroutine_handle.destroy при использовании Coroutines в C ++ 20? ⇐ C++
Программы на C++. Форум разработчиков
1753287569
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
Подробнее здесь: [url]https://stackoverflow.com/questions/79712041/why-does-it-crash-when-calling-coroutine-handle-destroy-while-using-coroutines-i[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия