Locking in Variable

Hi all

At page number 205, the book said that “Since Variable implements locking internally, you’re safe even if updates come from a background thread”. Could you please help me to clarify what is the locking in Variable?

Thanks

@fpillet Can you please help with this when you get a chance? Thank you - much appreciated! :]

@cuong1112035 internally, Variable uses lock to protect the value during get / set operations, so it is completely thread safe. It means that while you are reading the variable, no other thread can write it – the write would occur either before or after.

So technically, if two threads keep a reference to the same Variable there is no problem for a reader thread to receive updates from a writer thread.

Is that more clear for you? You can also check out the source code for Variable, it is not complicated to read.

1 Like

I got it, thanks so much

This topic was automatically closed after 166 days. New replies are no longer allowed.