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

Author Topic: Getting my thread to stop when I close my window  (Read 1330 times)

0 Members and 1 Guest are viewing this topic.

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Getting my thread to stop when I close my window
« 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;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32451
    • View Profile
    • SFML's website
    • Email
Re: Getting my thread to stop when I close my window
« Reply #1 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.
Laurent Gomila - SFML developer