- Code: Select all
void Lock()
{
while (m_lock.exchange(1))
{
std::this_thread::yield();
}
#endif
}
apparently this secund method is way more efficient
- Code: Select all
void Lock()
{
dUnsigned32 test = 0;
while (!m_lock.compare_exchange_weak(test, 1))
{
std::this_thread::yield();
test = 0;
}
#endif
}
the reasoning for this is that in a system where there are not too many thread contentions, (more threads than cores hit that lock) the thread that acquire the look write to memory a 1 and that has to go to system memory.
the threads that go to spin the they read memory from cache and see there the value is equal to expected so they to not write to memory and the memory channel for the memory location doesn't block other threads.
while the first method using m_lock.exchange(1) always writes to system memory therefore blocking other thread over the entire system.
has any one experimented with this?