Re: Instabilities with multi threaded application



I've just been browsing the g_idle_add function source code. This
function calls g_idle_add_full. g_idle_add_full creates a new idle
source and attaches it to the main context using g_source_attach.

The cool thing about it is that the context access is locked, through a
mutex, inside the g_source_attach function, therefore i think it's safe
enough to do it the way I'm doing.

Am I missing something? Or doesn't the mutex keep thing safe?

On Fri, 2004-07-23 at 18:21, Tristan Van Berkom wrote:
> Tiago Cogumbreiro wrote:
> [...]
> > Ouch, i was just doing this in the other thread:
> > 
> > MyGObject *self = MY_OBJECT(data);
> > /* push current state to queue */
> > g_async_queue_push(self->queue, GINT_TO_POINTER(new_state);
> > /* now start a g_idle call */
> > g_idle_add (on_event, data);
> > 
> > But, strangely enough, it works like a charm. I'll read the main event
> > loop more throughly.
> 
>      Hmmm, but you are not running a mainloop in your thread are you,
> in which case your `on_event' function is getting called in your
> parent thread, it looks neat and all, but I have a feeling that its
> unsafe to modify a GMainContext from another thread, there is probably
> a race condition in your code where both threads access the main
> context at the same time and produce that age old "unpredictable behaviour".
> 
> In the thread I pointed out in the previous mail, we discuss implementing
> an event source which pops elements off of a GAsyncQueue, where the thread
> that pushes onto the queue, calls g_main_context_wakeup() on the context
> of the recieving thread.
> 
> Cheers,
>                             -Tristan
> 




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