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

Author Topic: Condition Variables  (Read 3775 times)

0 Members and 1 Guest are viewing this topic.

Cornstalks

  • Full Member
  • ***
  • Posts: 180
    • View Profile
    • My Website
Condition Variables
« on: May 29, 2013, 02:08:30 am »
Condition variables would be incredibly useful with multithreaded programming. Can we please get them? (I know, not now, but at least in the future?)

Cornstalks

  • Full Member
  • ***
  • Posts: 180
    • View Profile
    • My Website
Re: Condition Variables
« Reply #1 on: May 29, 2013, 04:13:39 am »
Here's an example of what I have in mind (quickly made, if that's not obvious):
Code: [Select]
template <typename T>
struct Cond // should be non copyable
{
    Cond(pthread_mutex_t& mutex) : signaled(false), mutex(mutex)
    {
        pthread_cond_init(&cond, NULL);
    }

    ~Cond()
    {
        pthread_cond_destroy(&cond);
    }

    void set(const T& v)
    {
        val = v;
    }

    const volatile T& get() const
    {
        return val;
    }

    void setAndSignal(const T& v)
    {
        set(v);
        signal();
    }

    void signal()
    {
        pthread_mutex_lock(&mutex);
        signaled = true;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }

    void waitForSignal()
    {
        pthread_mutex_lock(&mutex);
        if (!signaled)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        pthread_mutex_unlock(&mutex);
    }

    volatile T val;
    bool signaled;
    pthread_cond_t cond;
    pthread_mutex_t& mutex;
};

And here's a sample usage:
Code: [Select]
pthread_mutex_t mutex;
Cond<int> cond(mutex);

void* getValueThread(void*)
{
    printf("Please hit space and press enter\n");

    cond.setAndSignal(getchar() == ' ');

    return NULL;
}

void* watchValueThread(void*)
{
    cond.waitForSignal();

    if (cond.get())
        printf("You hit space!\n");
    else
        printf("You did NOT hit space!\n");

    return NULL;
}

I imagine something similar could be done with Win32 condition variables.

We're going to need condition variables for Android (the OS calls into application functions from an external thread, and condition variables are needed to sync things up properly). I'll probably implement something along these lines in Sonkun's Android branch, but I'm hoping we might find support for them in the official SFML repo.

Edit: I have a decent version done now (Unix and Win32, though not compatible with XP); I'm just documenting it.
« Last Edit: May 29, 2013, 06:58:05 am by Cornstalks »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Condition Variables
« Reply #2 on: May 29, 2013, 08:02:06 am »
I'm sure it has already been discussed, have you searched before asking?
Laurent Gomila - SFML developer

Cornstalks

  • Full Member
  • ***
  • Posts: 180
    • View Profile
    • My Website
Re: Condition Variables
« Reply #3 on: May 29, 2013, 04:36:18 pm »
I'm sure it has already been discussed, have you searched before asking?
The one and only thing that seemed close to what I'm requesting is this which was closed without a real reason being stated (perhaps because it wasn't posted in the forums?). I'm unable to find any actual discussion.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Condition Variables
« Reply #4 on: May 29, 2013, 04:43:11 pm »
Maybe it was not specifically about condition variables, but rather about more threading features in general.

The answer is that SFML is not a threading library, it only implements what it needs. There are dedicated threading libraries that do this job much better than SFML would (boost, C++11 standard library, ...), so there's no reason for me to reinvent the wheel.
Laurent Gomila - SFML developer

Cornstalks

  • Full Member
  • ***
  • Posts: 180
    • View Profile
    • My Website
Re: Condition Variables
« Reply #5 on: May 29, 2013, 05:03:35 pm »
The answer is that SFML is not a threading library, it only implements what it needs. There are dedicated threading libraries that do this job much better than SFML would (boost, C++11 standard library, ...), so there's no reason for me to reinvent the wheel.
So in the case where it's needed in the Android code, would you (if you were writing it) just use pthreads directly (so you use pthread threads, mutexes, and condition variables, avoid all the sf::Thread stuff), or would you write a condition variable class for SFML and then use sf::Thread, sf::Mutex, sf::ConditionVariable, etc?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Condition Variables
« Reply #6 on: May 29, 2013, 07:05:38 pm »
As long as it remains platform specific, and it's not needed in the generic code, I think I would use directly what the platform offers.
Laurent Gomila - SFML developer