dimanche 29 mars 2015

locking with compare and swap on a boolean

I am experimenting the atomic_compare_and_swap function to do a basic lock on a std::atomic.


The behaviour I was expecting is that the second thread i.e 'Consume' would remain blocked in the while-loop at the beginning of Access::get() for all the time the atomic lock_ is set to true.


Due to the sleeps I've introduced it is always the first thread 'Produce' to set the atomic to true to prevent the second thread to proceed.


Unfortunately this is not the case and I can see that the second thread executes immediately and doesn't remain blocked at all.


What am I doing wrong?


I am using g++4.9 on Lubuntu



class Access
{
atomic<bool> lock_;
bool UNLOCKED_;

public:
Access() : lock_(false), UNLOCKED_(false){}

void set()
{
// lock
while(!atomic_compare_exchange_strong(&lock_, &UNLOCKED_, true)) {}
this_thread::sleep_for(std::chrono::seconds(20));
cout << "set" << endl;
}

void get()
{
// lock
while(!atomic_compare_exchange_strong(&lock_, &UNLOCKED_, true)){}
cout << "get" << endl;
}
};

Access gTest; // global

void Produce() { gTest.set(); }

void Consume() { gTest.get(); }

int main() {
thread producer(Produce);
this_thread::sleep_for(std::chrono::seconds(3));
thread consumer(Consume);
producer.join();
consumer.join();
return 0;
}

Aucun commentaire:

Enregistrer un commentaire