Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
is this a thread safe stack
Hi Please refer code below: Can I say that there will not be any issue with stack due to thread data race? Any suggestions? https://code.sololearn.com/cT88N1pEcR8y/?ref=app
2 Answers
+ 1
The implementation looks thread safe to me but with data structures like stack, it's very easy to mess up stuff as the interface of the structure that we usually have for stack is itself prone to race conditions.
Not only can you never confirm values returned from top() or size() as values returned by them might get invalidated as soon as some other thread calls push() or pop() before you use those values. Locking every function entirely would also lead to little to no gain in performance by going parallel.
0
Thanks Arsenic
I got your point about issues... what's the solution then ? Any hint or there is no soultion and we just have to live with this limitations ?
Also, one query in your response. Does stack also invalidate ? I thought stack has no invalidate as all elements are never relocated but just pointer changes