Hello !
I've been playing with sf::Thread and found out something that disturbed me. Using slightly modified code from the tutorials :
#include <SFML/System.hpp>
#include <iostream>
class MyThread : public sf::Thread
{
private :
virtual void Run()
{
// Print something...
for (int i = 0; i < 10; ++i)
std::cout << "I'm the thread number 2" << std::endl;
}
};
int main()
{
// Create an instance of our custom thread class
{
MyThread Thread;
// Start it !
Thread.Launch();
}
// Print something...
for (int i = 0; i < 10; ++i)
std::cout << "I'm the main thread" << std::endl;
return EXIT_SUCCESS;
}
... then the content of MyThread::Run is never executed.
Looking at the source of sf::Thread, it seems that Wait() is called in sf::Thread's destructor, so one could expect that the thread waits until Run() returns before the end of the destructor.
But by that time, we are in sf::Thread's destructor, and no derived function can be called : the thread now runs sf::Thread::Run(), which does nothing and terminates the thread.
As a check : turn sf::Thread::Run() into a pure virtual function and you'll get an error message saying a pure virtual function has been called.
I'm not sure this is the expected behavior, because you don't have this problem with free functions.
More intriguing, if you write :
#include <SFML/System.hpp>
#include <iostream>
class MyThread : public sf::Thread
{
private :
virtual void Run()
{
// Print something...
for (int i = 0; i < 10; ++i)
std::cout << "I'm the thread number 2" << std::endl;
}
};
int main()
{
int n = 5;
// Create an instance of our custom thread class
{
MyThread Thread;
// Start it !
Thread.Launch();
for (int i = 0; i < n; ++i)
std::cout << "I'm the main thread" << std::endl;
}
// Print something...
for (int i = 0; i < 10 - n; ++i)
std::cout << "I'm the main thread" << std::endl;
return EXIT_SUCCESS;
}
... with n = anything greater than 0, then sf::Thread actually executes the whole MyThread::Run() function :shock:
I'm using SFML 2.0 (15/06/2010) on Windows XP with MinGW.
Any clue ?