Lemme make sure I am understanding the problem...
- ThreadSafeProcessor owns a thread
- EventQueue derives from ThreadSafeProcessor
- The thread uses EventQueue
- Upon destruction of the EventQueue object, it is destroyed while the thread is still using it.
Am I understanding correctly?
I would separate this so that one object owns the thread and does the logic. IE, rather than having ThreadSafeProcessor be the parent to the EventQueue, it would own it. That way it can ensure the order of destruction.
Something similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
template <typename T>
class EventProcessor // or something
{/*...*/};
template <typename T>
class EventQueue : public EventProcessor<T>
{/*...*/};
template <typename T>
class ThreadSafePrcoessor
{
//...
EventProcessor<T> processor; // own the processor
std::thread thread; // own the thread
};
|
This is better anyway because then outside code does not have direct access to an object that is operating in another thread. ThreadSafeProcessor then controls all interaction with the processing thread (and the object that is running in it), and can put locks up where necessary.
Likewise, the event handler does not need to concern itself with any thread issues. It can just do its work.