dimanche 16 octobre 2022

use enum in std::atomic::compare_exchange_strong()

I tried to use std::atomic::compare_exchange_strong() with enum but it raise this error

cannot bind non-const lvalue reference of type ‘std::__atomic_base<int>::__int_type&’ {aka ‘int&’} to an rvalue of type ‘std::__atomic_base<int>::__int_type’ {aka ‘int’}

here is part of my code, simplified for the key.

enum LockStatus
{
    readed, 
    reading,
    writing,
    written
};

int main()
{
    atomic_int **lock;
    lock=new atomic_int* [n];
    for(int i=0; i<n ;i++)
    {
        if(lock[i]->compare_exchange_strong(readed,writing))
        {
           //something to do 
        }
    }
}

I think this problem is caused by enum is a right value but compare_exchange_strong need a left value, if I define these LockStatus as int it will work,but I was wondering if there is a more elegant why to solve this problem?

Thank you for your answer!

Aucun commentaire:

Enregistrer un commentaire