vendredi 1 avril 2022

Spinlock code using atomic_flag in C++ is not compiling in Mac

I tried to write a simple Spinlock code in C++ but the code is not getting compiled in the Mac but is compiling on other gcc compilers.

#include<iostream>
#include<thread>
#include<atomic>
using namespace std;

class SpinLock 
{
    public:
        atomic_flag flag;
        SpinLock() : flag(ATOMIC_FLAG_INIT) {}
        void lock()
        {
            while(flag.test_and_set());
        }
        void unlock()
        {
            flag.clear();
        }
};

SpinLock spin;

void critical_section()
{
    spin.lock();
    for(int i = 0 ; i < 10 ; i++)
    {
        cout << i << " ";
    }
    cout << endl;
    spin.unlock();
}

int main()
{
    thread t1(critical_section);
    thread t2(critical_section);
    t1.join();
    t2.join();
}

I tried checking version of clang installed in my machine at the version is: Apple clang version 13.1.6 (clang-1316.0.21.2) Target: arm64-apple-darwin21.4.0 Thread model: posix

Can any one help me in the fact that why code is not getting compiled?

Aucun commentaire:

Enregistrer un commentaire