Re: Instabilities with multi threaded application



On Fri, 2004-07-23 at 10:06 -0400, Colin Walters wrote:
> On Fri, 2004-07-23 at 14:27 +0200, Philip Van Hoof wrote:
> 
 
> 
> Why aren't you using a GAsyncQueue?

Being stunned about the fact that I didn't know of it's existence and
filled with the fear that I'd been reinventing the wheel .. again, I
started reading about the GAsyncQueue-functionality and missed one major
feature, I cannot assign a priority to the queued items.

The code that I pasted on this mailinglist does not yet have that
priority-stuff. But this has been added later and has become a must-have
feature for what I am building here.


> > 
> > static void * thread_main_func (void * data)
> > {
> > 	Queue *queue = (Queue*)data;
> > 
> > 	while (queue->items)
> 
> This is unsafe.  Use: while ((item = g_async_queue_try_pop(queue)))
> 
> > 	if (g_list_length (queue->items) == 1)
> > 	{
> > 	 pthread_create (&queue->thread, NULL, thread_main_func, queue);
> > 	}


> This seems like a race condition that will create multiple consumer
> threads in some cases (not to mention it being unsafe to call
> g_list_length on a list being modified by another thread).  

I noticed that and for that reason have created a mutex that locks calls
to the GList. So the thread that will add items to the GList will have
to wait for that lock to finish. The g_list_length()-call has been put
between the lock and the unlock statement of that mutex (GMutex).

> In that
> case, you will certainly have a bigger problem as multiple consumer
> threads try to process an item (item access isn't locked).

It is now..


> > While most of the times this works perfectly, I am getting 
> > 
> > Xlib: unexpected async reply (sequence 0x46c)!
> > 
> > When I click the GtkDrawingArea to fast. The thread which is created to
> > process queued items will get killed by a segmentation error.
> 
> Use a GAsyncQueue, and only create the thread once, and have it simply
> block on item retrieval.

Indeed, and at this moment it's working. I am not using the GAsyncQueue-
functionality but will investigate whether or not I can use (a portion)
of it for this application.

If somebody has a suggestion on how I can use priorities (so that the
item with the highest priority will get selected next), that would be
great.


Thanks

-- 
Philip Van Hoof, Software Developer @ Cronos
home: me at freax dot org
gnome: pvanhoof at gnome dot org
work: Philip dot VanHoof at Cronos dot Be
http://www.freax.be, http://www.freax.eu.org




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