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

Author Topic: Odd behaviour of thread  (Read 2972 times)

0 Members and 1 Guest are viewing this topic.

Silverlan

  • Newbie
  • *
  • Posts: 1
    • View Profile
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:
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.

Drektar

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Odd behaviour of thread
« Reply #1 on: December 13, 2010, 03:08:23 pm »
Quote
void CreateThread()
{
    TestThread thread;
    thread.Launch();
}

thread is destroy at the end of the CreateThread function so he don't have enough time to start.

If you do :
Code: [Select]
void CreateThread()
{
    TestThread *thread = new TestThread();
    thread->Launch();
}

It suppose to work but how you will delete the thread?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Odd behaviour of thread
« Reply #2 on: December 13, 2010, 04:06:55 pm »
You can have the thread delete itself when it's finished, though then that's a task model for your threading.

Anyway just make your thread object global or anything that will make it persist until not needed anymore. Also if you don't create more threads dynamically throughout the program but only at the start you can dynamically allocate memory(new) and forget about deleting them. When your application exits the OS will free any remaining memory for you. I know it's called bad programming but it is practical.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

blizter

  • Newbie
  • *
  • Posts: 7
    • View Profile
Odd behaviour of thread
« Reply #3 on: June 11, 2011, 11:34:41 pm »
I had this problem and Drektar solution fixed it.

The created thread was running but not threaded. Weird.

 

anything