Xyzk
Xyzk

Reputation: 1332

Can I add object to queue withour worrying it will break in multi thread application?

I have a producer, and multiple receivers. All products are added to the queue, from which they can be later taken away. The question is, do I need to check every time I want to add object to the queue if it's not already used, assuming I use semaphores to block any receiver from accessing it when it's empty?

Upvotes: 0

Views: 74

Answers (1)

Hampton Terry
Hampton Terry

Reputation: 354

If you have multiple threads using the same object potentially at once, yes you will to use a lock or semaphores to make sure only one thread is acting on the object at once.

Depending on what language you are using, I believe some already have this implemented for you. You can create data structure objects that are thread-safe and have the locking semantics already taken care of for you.

Upvotes: 1

Related Questions