is this a thread safe stack | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

27th Dec 2022, 12:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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.
1st Jan 2023, 2:08 AM
Arsenic
Arsenic - avatar
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
2nd Jan 2023, 9:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar