the following code acquire a mutex using lock_cmpxchg on an index of an array, then two threads write and read to the same index, but still thread sanitizer say there is a data race. how can I tell him the fact there is a lock between the threads because it seem it does not detect it.
aquire_mutex(bool* lock_ptr)
{
retval = _lock_cmpxchg_8bit(0, 1, lock_ptr); //lock_cmpxchg implemented in inline assembly
return (retval == 0) ? 1: 0;
}
foo()
{
if (aquire_mutex(global_lock[index])!= 1)
{
return error;
}
uint x = array[index];
...
array[index] = random_value;
}
the flow is:
a lot of threads are running with different indexes or the same index, the only data race appear if two threads are with the same index, but there is a lock for it.
but thread sanitizer warn that there is a read in line uint x = array[index];
by thread 1 and there is a write in line array[index] = random_value ;
Im not sure why it does detect data race, Thanks for the help!!