# Locking in Variable

**URL:** <https://forums.kodeco.com/t/locking-in-variable/42381>\
**Category:** RxSwift\
**Created:** [May 8, 2018, 2:04pm UTC](https://forums.kodeco.com/t/locking-in-variable/42381 "2018-05-08T14:04:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cuong1112035](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cuong1112035/32/101506_2.png) [@cuong1112035](https://forums.kodeco.com/u/cuong1112035)\
**Post date:** [May 8, 2018, 2:04pm UTC](https://forums.kodeco.com/t/locking-in-variable/42381/1 "2018-05-08T14:04:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [May 30, 2018, 3:24pm UTC](https://forums.kodeco.com/t/locking-in-variable/42381/2 "2018-05-30T15:24:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fpillet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fpillet/32/97677_2.png) [@fpillet](https://forums.kodeco.com/u/fpillet)\
**Post date:** [May 30, 2018, 7:33pm UTC](https://forums.kodeco.com/t/locking-in-variable/42381/3 "2018-05-30T19:33:40Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![cuong1112035](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cuong1112035/32/101506_2.png) [@cuong1112035](https://forums.kodeco.com/u/cuong1112035)\
**Post date:** [May 31, 2018, 3:07am UTC](https://forums.kodeco.com/t/locking-in-variable/42381/4 "2018-05-31T03:07:02Z")

</div>

> [@fpillet](#):
>
> You

I got it, thanks so much

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [October 22, 2018, 6:12am UTC](https://forums.kodeco.com/t/locking-in-variable/42381/5 "2018-10-22T06:12:44Z")

</div>

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