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

Author Topic: [SOLVED] Thread does not run in parallel  (Read 3854 times)

0 Members and 1 Guest are viewing this topic.

Pitxardo

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[SOLVED] Thread does not run in parallel
« on: October 08, 2012, 09:10:21 pm »
Here is a complete minimal example:

#include <iostream>
#include <SFML/Graphics.hpp>

void tfunction()
{
    long counter = 0;
    for(int i=0; i<10000; i++)
    {
        for(int j=0; j<10000; j++)
        {
            counter++;
        }
    }

    std::cout << "Counter: " << counter << std::endl;
}

int main ( int argc, char** argv )
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML thread example");

    sf::RectangleShape rectangle;
    rectangle.setSize(sf::Vector2f(20, 20));
    rectangle.setOutlineColor(sf::Color::Red);
    rectangle.setOutlineThickness(2);
    rectangle.setPosition(100, 100);

    window.setFramerateLimit(60);
    int secondCounter = 0;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        rectangle.rotate(10);
        window.draw(rectangle);
        window.display();

        secondCounter++;
        if(secondCounter>59)
        {
            std::cout << "Second elapsed." << std::endl;
            secondCounter = 0;

            sf::Thread thread(&tfunction);
            thread.launch();
        }
    }

    return 0;
}
 

This example shows an SFML window with a rectangle that rotates every 60 frames per second. Also, each every second, the program does a hard performance operation to calculate something big. I use a thread for this hard operation to avoid a delay or lag in the main process (the rotating rectangle).

But, as you can see if you run this little example, the main process is affected by the hard operation even when the process is launched in a parallel thread.

What am I doing wrong?
« Last Edit: October 08, 2012, 09:45:45 pm by Pitxardo »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Thread does not run in parallel
« Reply #1 on: October 08, 2012, 09:18:03 pm »
Well you're declaring the sf::Thread object inside the if-scope, thus the thread as an object can only life inside that scope. If you get to the end of the if-scope C++ will destroy the sf::Thread object and the sf::Thread object calls wait() in it's destructor, thus the main thread waits until the other thread has finished running and then proceed.
You need to declare the sf::Threads object outside the main loop or use a container that holds the sf::Thread objects outside the main loop scope. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Pitxardo

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Thread does not run in parallel
« Reply #2 on: October 08, 2012, 09:25:18 pm »
Problem solved!  ;D


As you said, I have put the Thread object out of the scope and now runs properly :)

Thank you so much!!!

 

anything