문제

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?

도움이 되었습니까?

해결책

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

다른 팁

From the documentation:

BlockingQueue implementations are thread-safe.

Therefore, you don't need a lock.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top