https://kotlinlang.org logo
#rx
Title
# rx
u

ursus

04/11/2020, 7:32 PM
is this thread safe? It is not, right?
z

Zach Klippenstein (he/him) [MOD]

04/13/2020, 5:30 PM
What do you mean by “thread safe”? You can call
value
and
accept
on a
BehaviorRelay
from multiple threads just fine. However if you call your
newJobs = …
update logic from multiple threads concurrently, there’s a race and your set could lose items. This can happen because both threads can read the relay’s value at the same time, each add their own `downloadJob`s, and then the first one to call
accept
will get overwritten by the second one.
If you wrap your update logic in some sort of critical section (e.g. using a
Mutex
), then it should be safe.
u

ursus

04/13/2020, 6:33 PM
yes the append is obvious, I meant the accept it self
because of http://jakewharton.github.io/RxRelay/com/jakewharton/rxrelay2/Relay.html
All methods except accept(T) are thread-safe. Use toSerialized() to make it thread-safe as well.
but naivelly looking at the source, it looks thread safe
z

Zach Klippenstein (he/him) [MOD]

04/13/2020, 11:02 PM
Ah, so I was just wrong. I think that method is not safe because different subscribers could get values in different orders? But I’m not sure if that’s actually part of the rx contract.
u

ursus

04/13/2020, 11:32 PM
weird is that's javadoc from base Relay and only copied to BehaviorRelay
3 Views