Alternative to exceptions when attempting to pop an empty stack
A recent question on StackOverflow asked about whether or not a mutex is locked twice by the same thread without unlocking the mutex. See https://stackoverflow.com/questions/76890110/c-the-thread-repeatedly-locks-the-same-mutex-multiple-times?noredirect=1#comment135552146_76890110 The C++ source code presented in the question is: 1 #include <exception> 2 #include <memory> 3 #include <mutex> 4 #include <stack> 5 6 struct empty_stack : std :: exception 7 { 8 const char * what () const throw (); 9 }; 10 11 template < typename T > 12 class threadsafe_stack 13 { 14 private : 15 std :: stack < T > data ; 16 mutable std :: mutex m ; 17 public : 18 threadsafe_stack (){} 19 threadsafe_stack ( const threadsafe_stack & other ) { 20 std :: lock_guard < std :: mutex > lock ( other . m ); 21 ...