I have a small framework for working with threads. The main part is an object that is basically a mutex controlled std::queue that one thread pushes onto, and the other thread pops from.
This class cannot be templated, because the types of objects can vary in a single run. Currently, I created a dumb class:
class Object {
public:
Object(){}
virtual ~Object(){}
};
Any object that is to flow from thread to thread via this class must inherit from Object as it is what the std::queue holds. This works well, but I imagine there must be a better approach as this approach requires inheritance and many calls to dynamic_cast. Any ideas?
EDIT The pointers in this case are also smart pointers, so type information is important to be maintained.
Use std::queue<boost::any>. It will hold objects of any type.
And to get the object, you’ve to use a special cast function provided by boost itself: