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

Author Topic: Thread management or the like in SFML2  (Read 3702 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Thread management or the like in SFML2
« on: December 16, 2010, 11:57:59 am »
How would adding methods like sf::Thread::Suspend and sf::Thread::Resume to the thread class? Currently the only way to simulate this yourself is:

Code: [Select]
void MyThreadFunc()
{
        /* Some code */
        while(isSuspended)
        {
                sf::Sleep(0.1f);
        }
        /* more code */
}


But it's not the same, it's not ideal for when you want to make a Master/Worker model for your threads or use the threads as tasks. Then you would like to:
Code: [Select]
void MyThreadFunc()
{
        while(shouldExecute)
        {
                /* do work */
                Suspend(); // Wait until the master wants me again.
        }
}


It should be a simple wrap around on functions like SuspendThread in Windows.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Thread management or the like in SFML2
« Reply #1 on: December 16, 2010, 12:30:44 pm »
It seems like there's no way to do this on every platform.

You may find useful information and workarounds in this thread for example:
http://lists.trolltech.com/qt-interest/2007-01/msg00406.html

Note that all workarounds seem to involve wait conditions, which SFML doesn't provide, so you're still blocked :D
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Thread management or the like in SFML2
« Reply #2 on: December 16, 2010, 01:52:15 pm »
Damn :(

Thought pthreads also had support for it.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Thread management or the like in SFML2
« Reply #3 on: December 16, 2010, 02:40:59 pm »
I think you can do it with a mutex. But in any case, you'll have to insert "checkpoints" in your thread, you won't be able to stop it anywhere.

Something like this:
Code: [Select]
void MyTask::suspend()
{
    mutex.lock();
    suspended = true;
}

void MyTask::resume()
{
    mutex.unlock();
    suspended = false;
}

void MyTask::run()
{
    while (!stop)
    {
        // check for suspend
        suspendIfRequested();

        // continue...
        ...
    }
}

void MyTask::suspendIfRequested()
{
    if (suspended)
    {
        mutex.lock();
        mutex.unlock();
    }
}
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Thread management or the like in SFML2
« Reply #4 on: December 16, 2010, 03:06:42 pm »
It's not something I need right now. Was just doing something in class and saw that SFML didn't support it so thought I would check in with you :P

Though if it's task then it's no biggie, we want to suspend the thread after the task is done which doesn't really insert a lot of extra work.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Lalaland

  • Newbie
  • *
  • Posts: 4
    • View Profile
Thread management or the like in SFML2
« Reply #5 on: December 17, 2010, 03:25:37 pm »
I will assume here that you are using queue's of some sort to pass your instructions to the thread.

You might want to look into boost::thread for threading.
It is cross platform, commonly used,  has similar syntax to the coming c++0x standard, etc..

They also have lots of useful constructs, one of which, condition variables, seems to be the perfect thing you need.

A condition variable pauses a thread till another thread calls notify on it.

Here is an example use of a condition variable

Code: [Select]

{
   boost::scoped_lock lock(AMutexForAQueue); //locks a mutex

   while (ASharedQueue.empty()) //We cannot do work while the queue is empty
   {
       cond.wait(); //wait
   }

someData = ASharedQueue.pop();
}

//Do stuff with data here

 


While waiting the mutex will be released.

Here is how the code would look on the main thread side.

Code: [Select]


{
    boost::scoped_lock lock(AMutexForAQueue); //locks a mutex

    ASharedQueue.push(someTask); // push a task into the queue
}

cond.notify_one(); // start the thread

 


EDIT:  This method would also allow you to easily add more threads, as the condition variable is guaranteed to only notify one at a time.

 

anything