I need to create threads in cycle and when new thread is reaching some place of main thread code - it should be killed and memory should be freed. I’m using this to create threads in cycle.
//runner object
void ThreadRider::CallThread(APlayer* Caller, MMessage *Msg, float TimeDelay)
{
MessageToSend = Msg;
TimeToWait = TimeDelay;
CallingActor = Caller;
Thread = FRunnableThread::Create(this, TEXT("MessageThreadWorker"), 0, TPri_Normal);
}
To check stability I’m simply creating and Kill(true) 'ing thread in Tick() of actor which owns runner object.
After of 2-3 minutes of running tick() which is
MessageDelayThread->Thread->Kill(true);
MessageDelayThread->Thread = NULL;
MessageDelayThread->CallThread(this, Message, NextEventTime);
UE is throwing an exception at FRunnableThread::FreeTls().
More exactly - Assertion failed: ThreadID == FPlatformTLS::GetCurrentThreadId()
Why is that happening? I thought Kill(true) waits for stuff to complete so this error should not happen?