Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: window event Pulling - draw relation (2 threads)  (Read 1520 times)

0 Members and 1 Guest are viewing this topic.

lrx

  • Newbie
  • *
  • Posts: 29
    • View Profile
window event Pulling - draw relation (2 threads)
« on: May 12, 2013, 02:10:02 am »
Hello
Settings:
I've dug out my old code and decided to give multithreaded game engine approach a shot.
The idea is to run drawing loop and update+event loop in separate threads.

drawing is done in "new thread" while event pulling and updating is supposed to run in main thread.

Problem:
Why does my drawing loop slow down aprox. 50 times if I don't put update/event loop to sleep for a short duration?
Since the two should run independently of each other (no communication is done yet, no synchronization, nothing) ran on multi-core cpu, the only connection between these loops is that I draw to same RenderWindow as I pull events from.

In the end of the day I do plan to limit how many times a sec each of these loops executes, but that doesn't change fact that I wonder how they influence each other or if my though flow is flawed :P


Fairly minimal example
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

sf::RenderWindow window;
sf::View Camera;

void Draw()
{
        //time and fps variables
        int counter = 0;
        int fps = 0;
        sf::Text fpsText;
        sf::Clock clk;
        sf::Font font;
        sf::Time fpsTime = sf::Time::Zero;
        std::stringstream ss;

        font.loadFromFile("corbel.ttf");
        fpsText.setFont(font);

        //samething to draw
        sf::VertexArray va;
        va.setPrimitiveType(sf::Triangles);
        va.append(sf::Vector2f(10, 10));
        va.append(sf::Vector2f(100, 10));
        va.append(sf::Vector2f(100, 100));
        va[0].color = sf::Color::Red;
        va[1].color = sf::Color::Blue;
        va[2].color = sf::Color::Green;

        window.setActive(true);

        while(window.isOpen())
        {                              
                window.setView(Camera);

                window.clear();
                window.draw(va); //drawing of everything here

                window.setView(window.getDefaultView());

                //fps
                counter++;             
                fpsTime+=clk.restart();;
                if(fpsTime.asSeconds()>=1)
                {
                        fps = (int)(counter/fpsTime.asSeconds());
                        counter=0;
                        fpsTime = sf::Time::Zero;
                        ss.str(std::string());
                        ss.clear();
                        ss<<fps;
                        fpsText.setString(ss.str());
                }
                window.draw(fpsText);
                window.display();
        }
};


int main()
{      
        window.create(sf::VideoMode(1680, 1050),"");
        window.setVerticalSyncEnabled(false);
        window.setActive(false);

        //drawing thread start
        sf::Thread RenderThread(Draw);
        RenderThread.launch();

        //GameLoop
        while(window.isOpen())
        {
                //Event Handling
                sf::Event event;
                while(window.pollEvent(event))
                {
                        switch (event.type)
                        {
                                case sf::Event::Closed:
                                        window.close();
                                        RenderThread.wait();
                                        break;
                                case sf::Event::KeyPressed:    
                                {
                                        switch (event.key.code)
                                        {
                                                case sf::Keyboard::Escape:
                                                        window.close();
                                                        RenderThread.wait();
                                                        break;
                                                //more key events here
                                        default:
                                                break;
                                        }
                                        break;
                                }
                                default:
                                        break;
                        }
                }

                //update here

                //sleep to allow other thread to work
                sf::sleep(sf::milliseconds(3));
        }
}
 
« Last Edit: May 12, 2013, 03:40:49 am by lrx »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: window event Pulling - draw relation (2 threads)
« Reply #1 on: May 12, 2013, 10:39:28 am »
You said it in your comments:
//sleep to allow other thread to work

If you don't sleep, the main thread will take most of the available CPU time (for nothing!) and slowdown all other threads.
Laurent Gomila - SFML developer

lrx

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: window event Pulling - draw relation (2 threads)
« Reply #2 on: May 12, 2013, 03:16:57 pm »
Yes that's the conclusion I got from observation, but why would that affect other thread? shouldn't it be executed independently on other core?
It got to have something to do with event pulling, if I remove everything from //GameLoop and leave it empty or almost empty(adding int to 10k then 0 it), drawing thread will not slow down, even without sleep

Update:

turns out, without sleep drawing thread will work at ~70% of its original speed, if I click on desktop then back on my windows title bar (clicking on drawing area has no effect)

« Last Edit: May 12, 2013, 03:52:34 pm by lrx »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: window event Pulling - draw relation (2 threads)
« Reply #3 on: May 12, 2013, 06:22:11 pm »
Quote
Yes that's the conclusion I got from observation, but why would that affect other thread? shouldn't it be executed independently on other core?
You can't make this kind of assumptions so easily ;)
Laurent Gomila - SFML developer

 

anything