Tagged as

[C++/MFC] : Use a Thread Delegator for your threads

I always use a somewhat different approach, here is a class"consumer" that has embedded the thread call in a member function,the member function runs a virtual call, hence the derived classesmay implement functionality as well as passing arguments asmember data of the class. Maybe you're...

I always use a somewhat different approach, here is a class
"consumer" that has embedded the thread call in a member function,
the member function runs a virtual call, hence the derived classes
may implement functionality as well as passing arguments as
member data of the class. Maybe you're interested to add such a method
to embed a thread call in a class to the spectrum of alternatives:

Comments and Discussions

Class is good, no issues regardning that. But it is not flexible enough to "inline" where you just need to create a thread. Since this is just a tip/trick, I did not expand it - I have more ideas about this, that would allow to create threads even within classes, as sumple as calling a function.

Yes, it is more complex in the base class stated, but it is very simple to use it:
you just derive a class, say CSimpleConsumer, from CConsumer and implement the virtual method
"Execute" to do whatever you need taking into account the m_bStop boolean to exit cleanly.
You start the thread with base method "Start", you abort with "Abort", you stop with "Stop", etc...
in this way you have embedded a thread completely in a class per itself.
You instantiate the derived class and you have a new thread available, etc... .
Maybe you may interested in this simple way to embed a thread in a class. This is achieved
just by passing "this" to the argument of CreateThread and having the member "LocalThreadFunc"
reinterpret it and calling the pure virtual method of the class "Execute".