Re: Still confused on new thread starting idle functions to update UI.



On Tue, Dec 3, 2013 at 11:02 AM, David Buchan <pdbuchan yahoo com> wrote:
These darn threads and idle functions still baffle me. I'm sorry to be such a pest.
I want to periodically update a textview as new information comes available. Sometimes this information can 
come in quickly (roughly every tenth of a second). Each update is a single line of text.
The thread allocates memory for the character string, and the idle function does not free it. Instead, the 
thread free's the memory just before it stops. It waits a bit to make sure the idle function has finished 
using the memory containing the message.
This may seem awkward and unnecessary, but if I just use a single character string, it is possible for the 
thread to replace the contents of the string with the next message while an idle function is still working 
with the previous message.

Hi David,
I have two solutions.
The first would be to simply pass a strdup() of your message to
g_idle_add() and then free() it in the callback.
If you are concerned about memory fragmentation, another solution
would be to box up a GString with a GMutex. In your thread you would
lock the mutex and append to the string, and then unlock the mutex. In
the callback, lock the mutex, process the string (by which I think you
are just dumping into a GtkTextBuffer), clear the GString, and then
unlock the mutex.

Good luck :)


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