dimanche 14 février 2016

Third Party server with UE4 : part 4 : C++ : thread

UE brings you what you need to thread your program.

class POCSHIM_API UShimServer : public FRunnable

just extend the interface FRunnable, you will have to implement
    virtual bool Init();
    virtual uint32 Run();
    virtual void Stop();

Run will be the function which will be threaded, so you can put a while (1), with a break condition, mutex, and so on into.

I ve created a singleton in shimserver

in shimserver.h:
static UShimServer* getInstance();

static UShimServer *instance;


in shimserver.cpp
UShimServer*  UShimServer::getInstance() {
    if (!UShimServer::instance) {
        UShimServer::instance = new UShimServer();
    }
    return UShimServer::instance;
}

UShimServer* UShimServer::instance = nullptr;


In myGameInstance class, I ve created an intialize function, which retrieve the singleton of shimserver, and from it create and launch the thread.
once you create the thread, it will run automatically init on instance, and run().

Aucun commentaire:

Enregistrer un commentaire