SFML community forums

Help => General => Topic started by: vechestva on May 18, 2013, 01:23:03 pm

Title: Question about destructors of some classes.
Post by: vechestva on May 18, 2013, 01:23:03 pm
Good day.
I have a question.
Why destructors some classes:
sf::~TcpSocket, sf::~Thread
do not call methods:
sf::TcpSocke::disconnect(),  sf::Thread::terminate()
?
When using them appear some discomfort, for example:
sf::Thread* thread= new sf::Thread(&fooo);
thread.launch();
...
delete thread;
The thread is still active, you have to manually call thread->terminate().

ps: And yet, it would be nice to add the processing status for threads. (sf::Thread::Status)
Title: Re: Question about destructors of some classes.
Post by: Nexus on May 18, 2013, 01:30:38 pm
The thread is still active, you have to manually call thread->terminate().
No, you should not call terminate() in normal situations. sf::Thread::~Thread() invokes wait(), therefore it waits until the thread is finished. Just let your thread end regularly.

Concerning sf::TcpSocket, the base class destructor ~Socket() invokes close().


When using them appear some discomfort, for example:
sf::Thread* thread= new sf::Thread(&fooo);
thread.launch();
...
delete thread;
The discomfort here is to have new and delete, instead of adhering to RAII and using automatic variables:
sf::Thread thread(&foo);
thread.launch();


ps: And yet, it would be nice to add the processing status for threads. (sf::Thread::Status)
What is the "processing status"?
Title: Re: Question about destructors of some classes.
Post by: vechestva on May 18, 2013, 01:46:25 pm
The thread is still active, you have to manually call thread->terminate().
No, you should not call terminate() in normal situations. sf::Thread::~Thread() invokes wait(), therefore it waits until the thread is finished. Just let your thread end regularly.

Concerning sf::TcpSocket, the base class destructor ~Socket() invokes close().
Thanks, now I understand.

What is the "processing status"?
Whether running launch()?
I would like to use a list of pointers to thread (std::list<sf::Thread*>), but i have to use a pair. (std::list <std::pair<sf::Thread*, bool /*isLaunched*/>>) or a map.