is this thread safe? It is not, right?
# rx
u
is this thread safe? It is not, right?
z
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
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
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
weird is that's javadoc from base Relay and only copied to BehaviorRelay