Re: [gtk-list] newby stupid syncronization question...



On Mon, 23 Aug 1999, DROUIN Antoine STNA 7SB K232 p5766 wrote:

> I'm writing a gui that must wait for async messages in a blocking function.
> So i've created a second thread. When i get a message in the second thread, i 
> would like to notify the first thread thats sleeps in gtk-main and pass it the 
> message, in order to avoid having two threads using gtk.
> What is the best way of doing that ?

As far as the threading goes, you could use a mutex to protect a boolean
flag.   Define globally

    pthread_mutex_t signal_mutex;
    int signal_value = 0;

and initialise the mutex with

    pthread_mutex_init(&signal_mutex, NULL);

If you add a low-priority Gtk idle function using gtk_idle_add_priority()
containing code like

    int signalled = 0;

    pthread_mutex_lock(&signal_mutex);
    if (signal_value == 1) {
        /* other thread has signalled */

        signal_value = 0;
        signalled = 1;
    }
    pthread_mutex_unlock(&signal_mutex);

    if (signalled) {
        /* do whatever is required */

    }


then you can use the following code in the worker (non-Gtk) thread:

    pthread_mutex_lock(&signal_mutex);
    signal_value = 1;
    pthread_mutex_unlock(&signal_mutex);

to signal the Gtk thread.

Hope this helps.

Mark

-------------------------------------------------------------------------
Mark Shinwell                   Queens' College, Cambridge University, UK
Mail: mrs30@cam.ac.uk                   WWW: http://mrs30.quns.cam.ac.uk/
-------------------------------------------------------------------------



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]