Назад к задачам
Junior — Senior
9

Что выведет программа, использующая два std::async

Компании, где спрашивали:

СБЕР ДискСБЕР Диск
Получайте помощь с лайвкодингом в реальном времени с Sobes Copilot
Условие задачи

Нужно определить, какой текст появится в консоли после запуска следующего кода.

#include <future>
#include <iostream>
#include <thread>

constexpr int total = 3;

int main(int argc, char* argv[]) {
    std::async(std::launch::async, []() {
        for (int i = 0; i < total; ++i) {
            std::cout << 1;
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
    });

    std::async(std::launch::async, []() {
        for (int i = 0; i < total; ++i) {
            std::cout << 2;
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
    });

    return 0;
}