Re: Threads & independant main loops/contexts



So my problem - cut down to one essential - is the following: I have
one thread which has to be independant of all others (as completely as
possible, speaking of the MainLoop and probably of the MainContext also
...), but it has to insert watches of io channels into the main loop of
a third thread.

I did something like that a month ago.

I have a parent thread which is `server' thread
and listens on an IPC channel for IO requests.

the parent thread adds the request to a request queue
like so:

g_mutex_lock(hm->queue_mutex);
g_queue_push_tail(hm->job_queue, (gpointer)node);
g_cond_signal(hm->jobs_queued);
g_mutex_unlock(hm->queue_mutex);


there is no `mainloop' in the child thread.
the child thread loops like so:


while (am_running) {

        g_mutex_lock(hm->queue_mutex);
        if ((node = g_queue_pop_head(hm->job_queue)) == NULL) {
            _ib_debug((__FUNCTION__ ": waiting on condition jobs_queued"));
            g_cond_wait(hm->jobs_queued, hm->queue_mutex);
            /* this should not be null */
            node = g_queue_pop_head(hm->job_queue);
        }
        g_mutex_unlock(hm->queue_mutex);


        /* write to underlying hardware */
        if (node) {
            switch (node->action) {
            case IBACTION_WRITE:
                write_node(node);
                break;

        .....


This code was to write to a device that takes
seconds at a time to write where the 
kernel module makes extensive use of
`schedule()' in order to ease execution.



Hope this helps.
                -Tristan


Axel Bock wrote:

Hi all,

I just dont get it. :-(

Please listen: I have three threads with three main loops (I intend to
use). These main loops I want to be independant, so they can do stuff
without blocking / interfering with each other. It seems not to be
sufficient just to create a new main loop in each of the threads, which
would be the most easy solution I can think of. Now I read a lot about
these GMainContext's, but these I dont really understand a bit :-((

So my problem - cut down to one essential - is the following: I have
one thread which has to be independant of all others (as completely as
possible, speaking of the MainLoop and probably of the MainContext also
...), but it has to insert watches of io channels into the main loop of
a third thread.

How can I realize this? I think there has to be a way, but I'm quite
blinded at the moment ...

Thanks for all help and greetings,

                Axel.
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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