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



On Tue, 3 Dec 2013 11:02:53 -0800 (PST)
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 observed bad behavior is that sometimes messages don't appear at
all, or appear twice in my textview. It's quite unpredictable. Sounds
like a race condition.

I spawn a thread from the main program - and from now on I don't talk
about main - and this new thread prepares messages in a character
string. The thread then passes a pointer to the character string to
an idle function so that the idle function can update a textview in
the UI. When done, the idle function stops itself by returning a
G_SOURCE_REMOVE boolean.

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.

But I lied...

Because there are several messages (roughly 30), the thread actually
allocates memory for an *array* of character strings. An index is
then used to specify which one we're using.

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.

It is awkward, and probably unnecessary.  Unless you have a very good
reason, that is not the way to do it.  Pass the idle function a string
allocated on the heap, and free it in the idle function when finished
with.  Any other way creates thread dependencies which the message
passing approach you have adopted is intended to avoid.

Chris


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