Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Threads - do i need to lock on reading data?

look at this code:

int data=5;

void Thread1()
{
    if(data==5)
    {
       //nothing
    }
}

void Thread2()
{
    if(data==2)
    {
       //nothing
    }
}

in this case, do i need to use EnterCriticalSection/MutexLock before if(data==..) ?

like image 735
Tenev Avatar asked Jul 13 '10 23:07

Tenev


1 Answers

If you are just reading the data then no locks required.

If you are writing the data AND you care about the order data is read then you need to use CS to make sure the ordering is correct. (Note if the object has a more complex state that is not updated in an atomic operation then you may care more about the ordering of reads/writes).

like image 129
Martin York Avatar answered Sep 24 '22 14:09

Martin York