dimanche 31 mai 2015

Lock-based function runs faster than lock-free! Why?

While I was practicing lock-based vs. lock-free concurrency, I realised that my lock-based function takes less time than a function with no synchronisation:

// A simple money transfer function
void transfer(unsigned int from, unsigned int to, unsigned int amount)
{       
    lock_guard<mutex> lock(m); // removing this line makes it run slower!
    accounts[from] -= amount;
    accounts[to] += amount;
}

Here is the coliru link of the full code. What might be the reason?

Note: I am aware of the data-race issue!

Aucun commentaire:

Enregistrer un commentaire