I need to spawn a thread when a certain trigger event is received inside of a class Foo. The trigger event is received by a Winsock server class that has a reference to the variable triggerEvent
.
bool Foo::HandleEvents()
{
while (1)
{
// Other things are done at the top of this loop
switch (triggerEvent)
{
case 'h':
{
// I instantiate an object here to do
// what I need to do in the thread.
// I use a pointer that is a private
// member of Foo.
thingMaker = new ThingMaker(params);
// Spawn a new thread here calling a
// function of ThingMaker and using thingMaker
break;
}
case ...: return true;
default: break;
}
}
}
Since the thread is local to its case in the switch
, I lose access to it on break
. I can't call join()
because I'm dealing with real-time processing and cannot wait for the thread to finish unless I know it's already done.
I recently asked a question about threading here regarding the same application and was told detach()
is bad practice; I also think my question was too vague as the solution offered ended up not fitting my needs and my application has since changed in architecture.
I have also tried to encapsulate the thread in short-life manager class that creates instances of ThingMaker but to no avail.
How do I go about this? I suspect my main issue is scope, but my options are limited. Foo::HandleEvents()
cannot be delayed at all or else I lose critical data.