greenrobot/EventBus

When does `postingState.isPosting` become false in the `post(Object event)` method?

sreramk opened this issue · 3 comments

The variable postingState is local to the thread retrieving it.

if (!postingState.isPosting) {

Therefore, it must always be unique to each concurrent (or parallel) thread running the same method.

When does the value of the if statement become false in the above condition? Assuming it does become false, why doesn't this cause any race conditions? Given that there are no locks used, and it is also not an atomic variable.

andob commented

as far as I understand the code:

When does the value of the if statement become false in the above condition?
before the if statement isPosting will always be false. seems like a safeguard.

Assuming it does become false, why doesn't this cause any race conditions?
well it probably would cause race conditions in that case.

however, if you look at cancelEventDelivery you'll see this variable is used in order to ensure some preconditions. so it's not useless.

Thanks for clarifying. I was trying to understand the code and that part was confusing. Even if it does not have any performance impact, removing the condition statement (assuming there is no other reason for having it) could make that part less confusing.

Thanks for clarifying. I was trying to understand the code and that part was confusing. Even if it does not have any performance impact, removing the condition statement (assuming there is no other reason for having it) could make that part less confusing.

race condition will appear with shared variable ,but PostingThreadState variable is in ThreadLocal.so it is unique for every thread , not a shared variable.
so it won't cause race condition.