static void stepping_worker()

in procgen/src/vecgame.cpp [103:142]


static void stepping_worker(std::mutex &stepping_thread_mutex,
                            std::list<std::shared_ptr<Game>> &pending_games,
                            std::condition_variable &pending_games_added,
                            std::condition_variable &pending_game_complete, bool &time_to_die) {
    while (1) {
        std::shared_ptr<Game> game;

        {
            std::unique_lock<std::mutex> lock(stepping_thread_mutex);
            while (1) {
                if (time_to_die) {
                    return;
                }
                if (!pending_games.empty()) {
                    game = pending_games.front();
                    pending_games.pop_front();
                    break;
                }

                pending_games_added.wait(lock);
            }
        }

        // the first time the threads are activated is before any step, just to initialize
        // the environment and produce the initial observation
        if (!game->initial_reset_complete) {
            game->reset();
            game->observe();
            game->initial_reset_complete = true;
        } else{
            game->step();
        }

        {
            std::unique_lock<std::mutex> lock(stepping_thread_mutex);
            game->is_waiting_for_step = false;
            pending_game_complete.notify_all();
        }
    }
}