yet another thread question



When the user presses a button, an idle function is begun to watch a flag which will tell it a message has 
been left in a string for it by a worker thread. The worker thread is then started. It will produce results 
to be displayed in a textview by the idle function. When the worker thread has results, it puts them into the 
string and sets the message_available_flag. The idle function sees the flag set, prints the results to the 
textview, and then clears the message_available_flag.

But my problem is that the worker thread is really fast at producing results to be displayed and the idle 
function sometimes misses messages.

Is there a way to cause my worker thread to pause while the idle function finishes putting the message in the 
textview?

I first thought of putting something like this into the worker thread just before it sets the 
message_available flag and puts new results into the string:
=================================
for (;;) {
  G_LOCK (message_available_flag);
  if (message_available_flag == 0) break;   // Last message has been sent to textview, idle func is ready for 
another.
  G_UNLOCK (message_available_flag);
}

and then we do the following

put new message in message string;

G_LOCK (message_available_flag);
message_available_flag = 1;
G_UNLOCK (message_available_flag);
=================================

But this locks-up because (I think) the G_LOCK mutex isn't letting the idle loop get a chance to clear the 
flag from printing a previous message.

The idle function never alters the string in any way. It's up to the worker thread to clear it each time it 
needs to put a new message into the string.



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