Search code examples
c++c++14stdatomic

error: use of deleted function ‘std::atomic<unsigned int>::atomic(const std::atomic<unsigned int>&)’


While trying to compile the code base of a project from a Git repository, I came across the following error:

DGSF-AvA/worker/serverless_gpu/svgpu_manager.cpp:90:78: error: use of deleted function ‘std::atomic<unsigned int>::atomic(const std::atomic<unsigned int>&)’
             printf("not my turn:  me %u  turn %u\n", current_id, current_turn);
                                                                              ^

I searched for the reason behind the issue and found a question.

Based on the question above and the solution stated, I assume the error to be caused by the line given below (in the file):

...
...
std::atomic<uint32_t> current_turn(0); //<------Suspected line causing the issue
uint32_t current_id(0);
std::mutex sched_lock;

ava_proto::WorkerAssignReply SVGPUManager::HandleRequest(const ava_proto::WorkerAssignRequest &request) {
    ava_proto::WorkerAssignReply reply;

    if (request.gpu_count() > 1) {
        std::cerr << "ERR: someone requested more than 1 GPU, no bueno" << std::endl;
        return reply;
    }

    sched_lock.lock();
    uint32_t q_id = current_id;
    current_id += 1;
    sched_lock.unlock();

    uint32_t gpu_mem = request.gpu_mem()[0];

    //std::cerr << "[SVLESS-MNGR]: API server request arrived, asking for schedule with memory " << gpu_mem << std::endl;
    while (true) {
        //if not our turn, wait
        if (current_turn != current_id) {
            printf("not my turn:  me %u  turn %u\n", current_id, current_turn); // <--- line which complains of the deleted function
            std::this_thread::sleep_for(std::chrono::milliseconds(50));
        }
...
...

I assumed the reason to be similar to what was mentioned in the linked question thread. But, the way current_turn is initialized here is suggested as a valid solution in the answer to the linked question.

I even tried out the few other options listed in the solution:

std::atomic<uint32_t> current_turn{0};
std::atomic<uint32_t> current_turn = {0};

But none of the above alternatives gets rid of the issue. I find that the data type of current_turn is std::atomic<uint32_t> (different from the data type std::atomic_int mentioned in the question)

Also, the project I am talking about uses CMake. The compiler standard is declared as follows:

set(CMAKE_CXX_STANDARD 14)

So, the project is built using C++14 instead of C++11(in the question linked). Can anyone help me with this issue?


Solution

  • std::atomic is not a primitive type and can't be used in printf, and it can't be copied. You might want to use current_turn.load() in printf.