Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Thank you! I get the idea, but the intricacies of the inter-thread synchronization are hard for me to follow in some spots. I'm sure this code is correct, but that's not obvious. I think Pablo's idea of designating one thread responsibe for handing the timeout is the key to reducing complexity. Commented Jan 19, 2023 at 23:05
  • 1
    P. S. Kudos for creating an actual runnable example, complete with main()! Commented Jan 19, 2023 at 23:06
  • 1
    @VioletGiraffe There is actually a thread dedicated to that in the above solution. It's implicitly created by the Timer class. I think it's a good idea which is why I borrowed it but by itself, I don't see how it solves the larger problem. That is you will end up with race conditions such as a thread filling up the buffer at the same time the timeout occurs. The solution here errs on the side of correctness. It can surely be improved and if you have any questions, I can walk you through it. Would a textual description or more comments help? Commented Jan 20, 2023 at 17:17
  • I think I understand it for the most parts, but some comments would be nice to see in the beginning of process() where locks are acquired and conditions are checked, e. g. is the order important for correctness. As far as I understand, writerLock should make sure there is no race condition in the case of one thread adding to the buffer while another is processing it. Commented Jan 20, 2023 at 17:29
  • 1
    @VioletGiraffe writerLock is serving two purposes in the current update. It's making it so that no two threads can try to write at the buffer at the same time which you may or may not need. The main reason I added it was to avoid the race condition I mentioned in my last comment. That is, if a thread is writing when the timer fires, you could end up a with one thread writing to the buffer while the other is processing (or after it is done.) I'll add some commentary to the answer to explain things a bit and some comments. Commented Jan 20, 2023 at 18:57