-1

So I have a single-threaded game engine class, which has separate functions for input, update and rendering, and I've just started learning to use the wonderful boost library (asio and thread components). And I was thinking of separating my update and render functions into separate threads (and perhaps separate the input and update functions from each other as well). Of course these functions will sometimes access the same locations in memory, so I decided to use boost/thread's strand functionality to prevent them from executing at the same time.

Right now my main game loop looks like this:

void SDLEngine::Start()
{
    int update_time=0;
    quit=false;
    while(!quit)
    {
        update_time=SDL_GetTicks();
        DoInput();//get user input and alter data based on it
        DoUpdate();//update game data once per loop
        if(!minimized)
            DoRender();//render graphics to screen
        update_time=SDL_GetTicks()-update_time;
        SDL_Delay(max(0,target_time-update_time));//insert delay to run at desired FPS
    }
}

If I used separate threads it would look something like this:

void SDLEngine::Start()
{
    boost::asio::io_service io;
    boost::asio::strand strand_;
    boost::asio::deadline_timer input(io,boost::posix_time::milliseconds(16));
    boost::asio::deadline_timer update(io,boost::posix_time::milliseconds(16));
    boost::asio::deadline_timer render(io,boost::posix_time::milliseconds(16));
    //
    input.async_wait(strand_.wrap(boost::bind(&SDLEngine::DoInput,this)));
    update.async_wait(strand_.wrap(boost::bind(&SDLEngine::DoUpdate,this)));
    render.async_wait(strand_.wrap(boost::bind(&SDLEngine::DoRender,this)));
    //
    io.run();
}

So as you can see, before the loop went: Input->Update->Render->Delay->Repeat

Each one was run one after the other. If I used multithreading I would have to use strands so that updates and rendering wouldn't be run at the same time. So, is it still worth it to use multithreading here? They would still basically be running one at a time in separate cores. I basically have no experience in multithreaded applications so any help is appreciated.

Oh, and another thing: I'm using OpenGL for rendering. Would multithreading like this affect the way OpenGL renders in any way?

Gatleos
  • 43
  • 5

1 Answers1

0

You are using same strand for all handlers, so there is no multithreading at all. Also, your deadline_timer is in scope of Start() and you do not pass it anywhere. In this case you will not able to restart it from the handler (note its not "interval" timer, its just a "one-call timer").

I see no point in this "revamp" since you are not getting any benefit from asio and/or threads at all in this example.

These methods (input, update, render) are too big and they do many things, you cannot call them without blocking. Its hard to say precisely because i dont know whats the game and how it works, but I'd prefer to do following steps:

  • Try to revamp network i/o so its become fully async
  • Try to use all CPU cores

About what you have tried: i think its possible if you search your code for actions that really can run in parallel right now. For example: if you calculate for each NPC something that is not depending on other characters you can io_service.post() each to make use all threads that running io_service.run() at the moment. So your program stay singlethreaded, but you can use, say, 7 other threads on some "big" operations

Galimov Albert
  • 7,269
  • 1
  • 24
  • 50