I have single producer putting jobs in a queue one by one, and another thread taking jobs away from the queue one by one. I saw somewhere online that it can be done locklessly. What is the right thing to do? Are lockless implementations generally harder to maintain? Assuming for this project performance does not matter too much, correctness is more important.
Asked
Active
Viewed 241 times
0
-
Try both a locking and a lock-free implementation and compare their performance on your real use case. – Kerrek SB Dec 28 '15 at 18:11
-
2You do not need a **mutex**. But you need **some** synchronization primitive for this, you can not simply push on one side and pop on another, if this is what you mean. – SergeyA Dec 28 '15 at 18:11
-
Well if you can do it locklessly and it is more efficient that that is what you should do. The rule of thumb is: *If you have a variable that can be accessed by more than one thread and at least one of them is a writer than you need synchronization.* – NathanOliver Dec 28 '15 at 18:12
-
After the edit, I am voting to close the question. 'Harder to maintain' is purely opinion-based. – SergeyA Dec 28 '15 at 18:17