#include <iostream>
#include <thread>
#include <mutex>

int counter = 1;
const int MAX = 10;
std::mutex mtx;
bool turn_even = false;  // false -> odd's turn, true -> even's turn

void print_even() {
    while (true) {
        std::lock_guard<std::mutex> lock(mtx);
        if (counter > MAX)
            break;
        if (turn_even) {
            std::cout << "Even: " << counter << std::endl;
            ++counter;
            turn_even = false;
        }
    }
}

void print_odd() {
    while (true) {
        std::lock_guard<std::mutex> lock(mtx);
        if (counter > MAX)
            break;
        if (!turn_even) {
            std::cout << "Odd: " << counter << std::endl;
            ++counter;
            turn_even = true;
        }
    }
}

int main() {
    std::thread t1(print_even);
    std::thread t2(print_odd);

    t1.join();
    t2.join();

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: