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

Author Topic: High CPU usage when using Threads!  (Read 5086 times)

0 Members and 1 Guest are viewing this topic.

hakeris1010

  • Newbie
  • *
  • Posts: 7
    • View Profile
High CPU usage when using Threads!
« on: February 25, 2015, 03:07:05 pm »
Hello.

I'm using SFML threads, and this makes very high CPU usage.  Windows task manager is used to measure the CPU usage.

When only main thread is running, CPU usage is only 1 %, but when 2 threads run, CPU gets as high as 25 %, 3 threads - 50% and so on!!

My CPU has 4 cores, so it seems that each additional thread consumes the whole core!

Why this happens, with 1 thread adding 25% CPU?

It's nothing related to the framerate or sf::sleep(), they make no effect to CPU.

There's a little experimental program which shows everything. When running this code CPU is 25%.
When using addidional threads (commented) then goes as high as 75 %.

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <SFML/Graphics.hpp>
    #include <SFML/System.hpp>

    using namespace std;

    sf::RenderWindow window;
    sf::Event ev;

    bool areEvents=false;
    bool Window_Closed=false;


    void drawZumthin()
    {
    for(int i=0; i<4; i++)
    {
        sf::CircleShape shape(50);
        shape.setFillColor(sf::Color::Green);
        shape.setPosition(i*100,0);

        window.draw(shape);
    }
    }

    void threadOne(int num)
    {
    long i=0;
    while(1)
    {
        if(i==0) cout<<"Thread no. "<<num<<endl;
        //sf::sleep(sf::milliseconds(20));
        i++;
    }
    }

    struct Chunk
    {
    int type;
    int tiles[64];
    };

    void loadNewChunks()
    {
    cout<<"Loading Chunks\n";

    Chunk chunks[25];

    for(int i=0; i<25; i++)
    {
        chunks[i].type=i;

        for(int j=0; j<64; j++)
        {
            chunks[i].tiles[j]=j-i;
        }
    }
    }

    void startWind()
    {
    window.create(sf::VideoMode(400,300),"test");
    window.setFramerateLimit(60);

    while(window.isOpen())
    {
        while(window.pollEvent(ev))
        {
            areEvents=true;

            if(ev.type==sf::Event::Closed)
            {
                window.close();
                Window_Closed=true;
            }
        }

        window.clear();

        drawZumthin();

        window.display();
    }
    }

    void getEvents()
    {
    cout<<"getEvents started\n";
    while(!Window_Closed)
    {
        if(areEvents)
        {
            bool newChunk=false;

            if(ev.type==sf::Event::TextEntered)
            {
                if(char(ev.text.unicode)=='w') cout<<"\nUp!\n\n";
                if(char(ev.text.unicode)=='s') cout<<"\nDown!\n\n";
                if(char(ev.text.unicode)=='a') cout<<"\nLeft!\n\n";
                if(char(ev.text.unicode)=='d') cout<<"\nRight!\n\n";

                newChunk=true;
            }

            if(newChunk) loadNewChunks();

            areEvents=false;
        }
    }
    cout<<"GetEvents Stopped.\n";
    }

    int main()
    {
    /*cout<<"Launching experimental threads\n";

    sf::Thread thr2(threadOne, 1);
    sf::Thread thr3(threadOne, 2);
    thr2.launch();
    thr3.launch();*/


    cout<<"Launching startWind()\n";

    sf::Thread thr1(startWind);

    thr1.launch();

    sf::sleep(sf::seconds(2));

    getEvents();

    return 0;
    }
   

Please help me with this issue. Thanks!
« Last Edit: February 25, 2015, 04:14:44 pm by eXpl0it3r »

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: High CPU usage when using Threads!
« Reply #1 on: February 25, 2015, 04:27:01 pm »
Use sf::sleep in loops.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: High CPU usage when using Threads!
« Reply #2 on: February 25, 2015, 04:29:56 pm »
What do you think happens if you tell your CPU to run as fast as possible? Do you think it runs as fast as possible if the load is at 1% or rather 100% (25% overall for 4 cores)?

A infinite loop is nothing else than to tell your CPU to run as fast as possible and that obviously maxes out the core.

For threads you render in, use VSync or the framerate limiter.
For other threads you can let them sleep a bit.

Also don't use threads, if you have barely any understanding of synchronization and well threading in general. It's a rather advanced topic with tons and tons of pitfalls. And if you don't have the right knowledge you'll end up not speeding up anything and most likely just introduce bugs.
« Last Edit: February 25, 2015, 04:31:49 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hakeris1010

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: High CPU usage when using Threads!
« Reply #3 on: February 25, 2015, 05:59:27 pm »
Now i added sf::sleep to the event checking thread.

But there is weird thing: when using  sf::sleep(sf::microseconds(1000)), or 1 millisecond, CPU usage goes down to zero, perfectly good.

But when sleeping for less than 1 millisecond, for example sf::sleep(sf::microseconds(900)), then CPU usage jumps as high as 25% !!!

How so small change in sleep time can make CPU usage so high? This might be a SFML bug.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: High CPU usage when using Threads!
« Reply #4 on: February 25, 2015, 06:17:24 pm »
But when sleeping for less than 1 millisecond, for example sf::sleep(sf::microseconds(900)), then CPU usage jumps as high as 25% !!!

How so small change in sleep time can make CPU usage so high? This might be a SFML bug.
Or... much more likely, you don't really understand what is going on. One thing I really hate is people who are unfamiliar with the library assuming that behaviour they don't expect is due to some bug. Before assuming such things, make sure you understand what is actually happening. Out of all the problems people post on the forum about, less than 1% tend to be caused by bugs, and those posts are often made by more experienced people.

Please don't take any offence from what I just said, I'm just trying to teach people not to use the term "bug" so liberally. Almost all the time, it is we (the developers/experienced users) who will tell you if something is a bug, and not the other way around.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: High CPU usage when using Threads!
« Reply #5 on: February 25, 2015, 10:54:09 pm »
@binary1248 well said