c++11 - Lock-based function runs faster than a no-locking one! Why? -
while practicing lock-based vs. lock-free concurrency, realised lock-based function takes less time function no synchronisation:
// simple money transfer function void transfer(unsigned int from, unsigned int to, unsigned int amount) { lock_guard<mutex> lock(m); // removing line makes run slower! accounts[from] -= amount; accounts[to] += amount; }
here coliru link of full code. might reason?
note: aware of data-race issue!
update: observation based on coliru. run same code on macbook pro (retina, mid 2012) 2,6 ghz intel core i7 16 gb 1600 mhz ddr3 , realised no-locking 1 runs twice faster lock-based one. lock-based 1 runs little bit faster on coliru consistently.
Comments
Post a Comment