[SOLVED] Irrlicht Responsiveness (is threading required?)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.

[SOLVED] Irrlicht Responsiveness (is threading required?)

Postby sl.Apollo » Thu Jan 26, 2012 5:36 am

Hi there,

This is probably a noob question, but so far I'm having no luck.

I'm currently using Irrlicht in combination with Boost, CEGUI, and libpq++ (a PostgreSQL database connector). Here is a screenshot of the test program:

Image

By clicking connect, a void function is called with a try/catch block:
cpp Code: Select all
void connectionOpen()
{
        try
        {
 
        pqxx::connection c("user=xxxx password=xxxx dbname=xxxx hostaddr=xxx.xxx.xxx.xxx port=xxxx");
    pqxx::work w(c);
        if (c.is_open() != true)
        {
                 std::cout << "Connection isn't open!" << std::endl;
        }
        else
        {
                 std::cout << "Connection is open!" << std::endl;
        }
    pqxx::result r = w.exec("SELECT * FROM demo");
    w.commit();
 
    std::cout << r[0][0].name() << std::endl;
    }
 
        catch (std::exception const& f)
        {
                std::cout << f.what() << std::endl;
        }
}

While the server is turned on, this is not a problem. It retrieves data from the server almost instantly. Others I've sent the app to have had similar results.

However, when the server isn't running, the rendering quits entirely and Windows says the application has stopped responding. After 4-5 seconds or so, it will come back and write to the Debug window saying that it could not find a connection.

My question is: Is there a way to continue to render frames in Irrlicht while waiting for responses in outside tasks?

I've tried 3 different things:
1) Putting the Irrlicht rendering into its own thread and sleeping others.
2) Rendering frames through the Boost I/O service after a timer has expired.
3) Checking for updates inside of the While loop.

The results from the first two have been: crashing, rendering one frame and then stopping, or switching between 2 frames repeatedly in an endless loop.

The third has simply not detected the changes, or it will quit rendering and then write to the Debug window after the connectionOpen() is finished.

Here is the current render code (from the third attempt):
cpp Code: Select all
void renderLoop()
{
        do
        {
 
                if (connecting == true)
                {
                        std::cout << "This is connecting!" << std::endl;
                        t.connectionThread.sleep(xt);
                }
                else
                {
                        std::cout << "This isn't connecting." << std::endl;
                }
 
                renderFrame();
 
            if(elapsed < frame)
        {
                        boost::this_thread::sleep(boost::posix_time::millisec((frame - elapsed)));
        }
                else
                {
                        boost::this_thread::sleep(boost::posix_time::millisec(6));
                }
        }
        while (stopEngine == false && device->run());
        device->drop();
 
 }


cpp Code: Select all
void renderFrame()
{
                if (device->isWindowMinimized() != true || device->isWindowFocused() == true)
                {
            // Timer function beginning.
                start = device->getTimer()->getTime();
 
            // Begin the Irrlicht scene.
                driver->beginScene(true, true, SColor(255,255,255,255));
 
                // Draw all Irrlicht assets.
                smgr->drawAll();
                guienv->drawAll();
 
                // Draw all Crazy Eddie's GUI assets.
                CEGUI::System::getSingleton().renderGUI();
 
                // End the scene. Rinse and repeat.
                driver->endScene();
 
                // Get the "must stop" state (thread-safe)
                engineStopMutex.lock();
                stopEngine=engineStop;
                engineStopMutex.unlock();
 
                // Timer function end.
                end = device->getTimer()->getTime();
        elapsed = end-start;
                }
 }


Any help would be greatly appreciated... Definitely stuck on this one. :) Am I missing something or is my code just written poorly?
Last edited by sl.Apollo on Fri Jan 27, 2012 1:45 am, edited 1 time in total.
sl.Apollo
 
Posts: 4
Joined: Sat Dec 10, 2011 8:15 am

Re: Irrlicht Responsiveness (is threading required?)

Postby smso » Thu Jan 26, 2012 6:20 am

It seems to me that the db connection is done in the main (gui )thread, is it?

cpp Code: Select all
t.connectionThread.sleep(xt);


As a rule, do irrlicht rendering in the main thread only and other slow process in a separate thread. IMO use qt4 since it integrates better with irrlicht and it also provides thread support. Search for qirrwidget in the forum for more info.


Regards smso
smso
 
Posts: 216
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Irrlicht Responsiveness (is threading required?)

Postby sl.Apollo » Thu Jan 26, 2012 7:34 am

smso wrote:It seems to me that the db connection is done in the main (gui )thread, is it?

cpp Code: Select all
t.connectionThread.sleep(xt);
No, sorry for not explaining that more fully. The connectionThread is a boost::thread object in a seperate class. The button issues a connectionThread.join() command which then executes the openConnection().

So it looks like this:
ClickedEvent > Join/start thread > Thread executes function

Regardless, it still hangs up the Irrlicht rendering While loop, even with the sleep command in place. I've also tried removing the conditions on the sleep for debugging purposes, but that seems to cause major lag (even with the connectionThread set to NULL) and it still doesn't register the new boolean value.

smso wrote:As a rule, do irrlicht rendering in the main thread only and other slow process in a separate thread. IMO use qt4 since it integrates better with irrlicht and it also provides thread support. Search for qirrwidget in the forum for more info.

Regards smso
Thanks for the suggestion; if I can't get CEGUI to work out then I'll definitely take another look at that. Didn't realize it could also be used under the LGPL. :D
sl.Apollo
 
Posts: 4
Joined: Sat Dec 10, 2011 8:15 am

Re: Irrlicht Responsiveness (is threading required?)

Postby hybrid » Thu Jan 26, 2012 9:06 am

Well, if you join the thread upon button click, it will still freeze once you click the button. You should start the thread via the button and do some check of a statue variable during the render loop. Joining the thread back to the main thread should be done on dialog removal, once the state is clearly defined to be connected or no server.
hybrid
Admin
 
Posts: 13943
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany

Re: Irrlicht Responsiveness (is threading required?)

Postby sl.Apollo » Fri Jan 27, 2012 1:36 am

hybrid wrote:Well, if you join the thread upon button click, it will still freeze once you click the button. You should start the thread via the button and do some check of a statue variable during the render loop. Joining the thread back to the main thread should be done on dialog removal, once the state is clearly defined to be connected or no server.

Thank you very much hybrid! That's exactly what the problem was; I was joining the thread too early after executing... Stupid overlook on my part. :oops:

No more all-night coding. :lol:
sl.Apollo
 
Posts: 4
Joined: Sat Dec 10, 2011 8:15 am


Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest