1
System / Odd behaviour of thread
« on: December 13, 2010, 02:56:24 pm »
I seem to be having a rather odd issue with threads. Maybe I still don't quite understand how they work, but this problem doesn't seem to make much sense at all.
Here's a simple recreation of my problem:
This program works exactly as I want it to, both the main thread and sub thread are running at the same time and print a message into the console respectively.
However strange things happen if I change the main and CreateThread functions to this:
All I did was remove the infinite loop from CreateThread and paste it into the main function after CreateThread is called.
I would think that this should do exactly the same as it did before, both threads should run at the same time, and the messages should both be printed into the console. This is, however, not the case.
Instead it completely ignores the newly created thread and only the main thread seems to keep running.
Why is that? What am I doing wrong?
Any help would be really appreciated.
I'm using SFML v1.6 if that makes any difference.
Here's a simple recreation of my problem:
Code: [Select]
#include <SFML/System.hpp>
#include <iostream>
class TestThread : public sf::Thread
{
private :
virtual void Run()
{
while(true)
{
std::cout << "Sub thread..." << std::endl;
sf::Sleep(0.1f);
}
}
};
void CreateThread()
{
TestThread thread;
thread.Launch();
while(true)
{
std::cout << "Main thread..." << std::endl;
sf::Sleep(0.1f);
}
}
int main()
{
CreateThread();
return EXIT_SUCCESS;
}
This program works exactly as I want it to, both the main thread and sub thread are running at the same time and print a message into the console respectively.
However strange things happen if I change the main and CreateThread functions to this:
Code: [Select]
void CreateThread()
{
TestThread thread;
thread.Launch();
}
int main()
{
CreateThread();
while(true)
{
std::cout << "Main thread..." << std::endl;
sf::Sleep(0.1f);
}
return EXIT_SUCCESS;
}
All I did was remove the infinite loop from CreateThread and paste it into the main function after CreateThread is called.
I would think that this should do exactly the same as it did before, both threads should run at the same time, and the messages should both be printed into the console. This is, however, not the case.
Instead it completely ignores the newly created thread and only the main thread seems to keep running.
Why is that? What am I doing wrong?
Any help would be really appreciated.
I'm using SFML v1.6 if that makes any difference.