1

I know that concurrent adds to an stl queue in c++ can cause issues, and the way to solve this is adding a mutex lock around all add/remove calls.

But I am programming in Java at the moment, and I'm using BlockingQueue. The documentation only says that the thread that calls put/take on a BlockingQueue object gets blocked implicitly until there's room to put/there's something to take respectively. However, it does not mention anything about concurrent put/take calls. Would I need to protect these with a mutex lock?

2 Answers 2

1

No, blocking-queues are thread-safe. From the docs:

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control

Sign up to request clarification or add additional context in comments.

1 Comment

I was a little unclear about that wording, so thanks for confirming!
1

From the documentation:

BlockingQueue implementations are thread-safe.

Therefore, you don't need a lock.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.