I make use of a templated class so that I only have to maintain a single class which can handle all the types I need (could be hundreds). Here's the functor for that class where everything is done.
template <class T>
void SomeClass<T>::operator()()
{
Connection *conn = getConn();
while (true) {
auto data = conn->poll(ms));
T t();
do_something_with(t, data);
}
}
And I execute each one in a thread.
SomeClass<MyObj1> obj1();
SomeClass<MyObj2> obj2();
// ...
SomeClass<MyObj99> obj99();
SomeClass<MyObj100> obj100();
std::thread t1(obj1);
std::thread t2(obj2);
// ...
std::thread t99(obj99);
std::thread t100(obj100);
As one can see, that's a lot of while loops running in the background.
Is there a solution/pattern to doing something like this where I can keep the templated classfixing typo, but somehow remove/merge the while loops in operator()() into a single while loop? The reason for removing all the while loops would be to reduce resources so that I can run this on small devices.
I'm wondering if it's possible to somehow make it so that multiple SomeClass's can inherit from a single instance of a base class that actually runs a single main loop. Though I can't imagine what that would look like.
Update: One thing I should add is that, the connection and receiving of data stays around for the life of the program.
Aucun commentaire:
Enregistrer un commentaire