I have a class
(class A
) that is using the monitor pattern for safe access to a std::deque
. Any time I need to simply add or remove an element I am fine.
But sometimes a different class
(class B
) needs to call in and get a reference to an element in the deque
in class A
.
I have been returning an iterator
to that element, but there's the chance that another thread could 'push' a new element into class A
and invalidate the iterator that the class B
is using. The only way I can think to prevent this is to lock a mutex
in class A
before returning the iterator
, and then have the class B
call another function when it's finished to release the mutex
, but this seems hacky.
Any ideas for a cleaner way?