SFML community forums

Help => System => Topic started by: Grimlen on September 24, 2012, 10:08:26 pm

Title: Getting my thread to stop when I close my window
Post by: Grimlen on September 24, 2012, 10:08:26 pm
I'm attempting right now to make it so when I close my window, the thread stops by passing in a boolean (running) variable to my thread. However...when I close my window the thread does not appear to stop until I close the console window. The main window (RenderWindow) closes as expected, but the console window does not....Not sure what to do here.
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

#include <SFML/Graphics.hpp>

void netThread(void *userData){
        bool running = *(bool*) userData;
        while(running)
            cout << running << endl;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        bool running = true;
        sf::Thread thread(&netThread, &running);
        thread.launch();

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

        window.clear();
        window.display();
    }
        running = false;
    return 0;
}
 
Title: Re: Getting my thread to stop when I close my window
Post by: Laurent on September 24, 2012, 10:29:32 pm
You copy the initial value (true) of the shared boolean to a local variable, so now you have two unrelated variables and the thread one will never change. You must keep a reference or pointer to the initial variable, not a copy.