'Can I replace std::async with c++20 coroutines?
I am developing a game.
In It, I have a gameloop function. This gameloop function is used during map loading. In it's update method it queries a state object which contains the map loading progress and a few other things.
The map loading itself is currently implemented via std::async:
std::atomic<int8_t> progress = 0;
auto level_future = std::async(std::launch::async | std::launch::deferred, load_level_data, std::ref(progress), level, game_state);
the function load_level_data occasionally updates progress during the loading/decoding process.
Now I wondered if I could replace std::async with a coroutine?
As far as I understood coroutines, I can just add co_yield statements through the function with the progress value and use it in my gameloop in a while loop until progress has the desired value?
Pseudocode:
<return type ?> load_level_data(level_file) {
read_file_into_buffer();
progress = 5;
co_yield progress;
decode_file();
progress = 10;
co_yield progress;
fill_runtime_data();
progress = 70;
co_yield progress;
finish_loading();
progress= 100;
co_yield progress;
??? return decoded level file here???
}
int progress = 0;
while(progress != 100)
{
progress = yield load_level_data("foo");
}
level = load_level_data("foo") // get decoded level?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
