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

Author Topic: Condition class (synchronization between threads)  (Read 5919 times)

0 Members and 1 Guest are viewing this topic.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Condition class (synchronization between threads)
« on: June 01, 2011, 09:24:00 pm »
Helloo,

While working on sfeMovie, I've realised I was missing some powerful feature in order to properly handle threads' synchronization : conditions.

A condition is an object able to wait (block the current thread) until this condition is satisfied. As for this Condition class, it relies on integer values and therefore is able to wait until the condition reaches the requested value.

I'm posting here before publishing this work on the wiki in order to get your feedback. Note that I've made searches on conditions recently only, thus I'm absolutely not a conditions' expert. Therefore your feedback, even on extremely precise points is really welcome :) .

This class has been written in order to be able to handle conditions without having to use a "big" dependency (eg. boost).

The Condition class is made of 6 files you can download here: Condition.zip. There's no specific configuration settings, just put these files in your project and have fun :) .

Here's a little use example :
Code: [Select]
#include <SFML/System.hpp>
#include <iostream>
#include "Condition.h"

Condition cond;

void threadFunc()
{
    std::cout << "Thread launched" << std::endl;

    cond.waitForValueAndRetain(0); // Wait until the Condition reaches state 0
   
    std::cout << "Condition validated" << std::endl;
    // Do some protected work
    // ...
   
    cond.release(1); // Release the Condition in state 1
}


int main()
{
    sf::Thread th(threadFunc);
   
    cond = 1; // Set the Condition's state to 1
    th.Launch();
   
    sf::Sleep(...);
    cond = 0; // Change the Condition's state
   
    return 0;
}
Want to play movies in your SFML application? Check out sfeMovie!

 

anything