As nobody knows how to make multi-threading with FRunnables(FRunnableThread becomes blocking! - C++ - Epic Developer Community Forums) I decided to make it with simple C++ threads. And…They’re blocking main thread too.
What’s wrong? Maybe I’m doing something improperly? Client just showing me black screen, and spamming in log.
Engine version 4.8.
Code is:
Client::Client()
{
this->running = true;
static std::thread t(startLoop, this);
this->trd = &t;
}
void Client::startLoop(Client* instance)
{
while(instance->running)
{
Sleep(1);
UE_LOG(TempLog, Warning, TEXT("Running thread...")); // For debug, this is spamming
//do stuff;
}
}
And calling constructor of Client class in overrided BeginPlay() in actor.
I am using Unreal’s threads for a custom TCP client with no issue. I have a Client with two FRunnable and two FRunnableThread instances. The Client has a Start() and Stop() method:
In the Start() method of my Client, I create and start the threads:
FRunnable *sender = new Sender(socket, sendQueue);
FRunnableThread *senderThread = FRunnableThread::Create(sender, TEXT("Sender"));
FRunnable *receiver = new Receiver(socket, receiveQueue);
FRunnableThread *receiverThread = FRunnableThread::Create(receiver, TEXT("Receiver"));
My FRunnable’s do sleep, but I am using the following function:
FPlatformProcess::Sleep(0.01);
My TCP Client is owned by an Information Actor (AInfo). In the BeginPlay() method of my Actor, I construct and Start() my TCP Client:
void AMyInfoActor::BeginPlay() {
client = TSharedPtr<Client>(new Client(IpAddress, Port));
client.Get()->Start();
}
This approach is working well for me. Not sure what issue you are having exactly, but maybe my example will help you.
Your example is working, thanks!