Jason918 commented on PR #16125: URL: https://github.com/apache/pulsar/pull/16125#issuecomment-1188866996
> > @lhotari The `data` array object mutation only happens in the code path `put() -> expandArray()`, where `put()` acquires the `tailLock`, and `expandArray()` acquires the `headLock`. So the `data` array object should be thread safe if you acquires either `tailLock` or `headLock` before accessing the object. > > `data` array elements are mutated and read with 2 different locks in the methods in `GrowableArrayBlockingQueue` class. For example, `poll` is guarded by `headLock` and `put` is guarded by `tailLock`. I don't see how that could be thread safe. There's a related StackOverflow answer https://stackoverflow.com/a/8978397 which states: > > > "This means that you can safely write to two different indexes concurrently. However you need to synchronize a write/read to the same index if you want to make sure the consumer thread sees the last value written by the producer thread." @lhotari OK, I get your point. It's the array elements, not the array object. This concern make sense. My guess on this is that there are happen-before relations here: - `put()` -> - `data[tailIndex.value] = e;//write Ops` --> - `SIZE_UPDATER.getAndIncrement(...) in put()` --> - `SIZE_UPDATER.get() in poll()` --> - `T item = data[headIndex.value];//Read Ops`. So the array element written by `put` is always visible to `poll`. Not 100% sure about this :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
