Draw against a fixed budget and report whether any request was capped.

Atomic Budget Coordination Report

atomic_budget_coordination_report.cpp
#include <atomic>
#include <iostream>
#include <string>

int main() {
    int requests = ;
    const int capacity = 2;
    std::atomic<int> budget{capacity};
    int granted = 0;
    int denied = 0;

    for (int i = 0; i < requests; ++i) {
        int current = budget.load();
        bool drawn = false;
        while (current > 0) {
            if (budget.compare_exchange_weak(current, current - 1)) {
                drawn = true;
                break;
            }
        }
        if (drawn) {
            granted += 1;
        } else {
            denied += 1;
        }
    }

    int spare = budget.load();

    std::string status;
    if (denied > 0) {
        status = "capped";
    } else if (spare > 0) {
        status = "spare";
    } else {
        status = "spent";
    }

    std::cout << "requests=" << requests
              << " granted=" << granted
              << " denied=" << denied
              << " " << status << std::endl;
    return 0;
}
#include <atomic>
#include <iostream>
#include <string>

int main() {
    int requests = ;
    const int capacity = 2;
    std::atomic<int> budget{capacity};
    int granted = 0;
    int denied = 0;

    for (int i = 0; i < requests; ++i) {
        int current = budget.load();
        bool drawn = false;
        while (current > 0) {
            if (budget.compare_exchange_weak(current, current - 1)) {
                drawn = true;
                break;
            }
        }
        if (drawn) {
            granted += 1;
        } else {
            denied += 1;
        }
    }

    int spare = budget.load();

    std::string status;
    if (denied > 0) {
        status = "capped";
    } else if (spare > 0) {
        status = "spare";
    } else {
        status = "spent";
    }

    std::cout << "requests=" << requests
              << " granted=" << granted
              << " denied=" << denied
              << " " << status << std::endl;
    return 0;
}
#include <atomic>
#include <iostream>
#include <string>

int main() {
    int requests = ;
    const int capacity = 2;
    std::atomic<int> budget{capacity};
    int granted = 0;
    int denied = 0;

    for (int i = 0; i < requests; ++i) {
        int current = budget.load();
        bool drawn = false;
        while (current > 0) {
            if (budget.compare_exchange_weak(current, current - 1)) {
                drawn = true;
                break;
            }
        }
        if (drawn) {
            granted += 1;
        } else {
            denied += 1;
        }
    }

    int spare = budget.load();

    std::string status;
    if (denied > 0) {
        status = "capped";
    } else if (spare > 0) {
        status = "spare";
    } else {
        status = "spent";
    }

    std::cout << "requests=" << requests
              << " granted=" << granted
              << " denied=" << denied
              << " " << status << std::endl;
    return 0;
}
bounded draw A fixed budget admits only as many draws as it holds. A `std::atomic<int>` budget starts at the capacity, and each request reads the current value and uses `compare_exchange_weak` to drop it by one while it is still positive, retrying on a spurious miss but never blocking. A request that finds the budget at zero is denied instead. The single deterministic pass lets the report count granted versus denied draws and label the section spare when capacity is left, spent when it lands exactly on zero, or capped when a request was denied.