> In article <[EMAIL PROTECTED]>,
>  Nicholas Schwarz <[EMAIL PROTECTED]> wrote:
>
> > Hi again,
> >
> > I'm seeking some advice about how to use threads with FLTK, preferably
> > without using FLTK's built-in lock and unlock features. A thread that opens 
> > a
> > socket will execute callbacks based on incoming messages. Those callbacks
> > have to update widget values. Is it possible to make a message queue using
> > FLTK's event handlers? I really don't like the idea of replacing Fl::run 
> > with
> > a loop that polls a condition variable, but don't know of another way to do
> > it.
>
> Yes, you can do that in the upcoming 1.1.8 (current 1.1.x from svn).
> You can use the built-in queue provided by Fl::awake/Fl::set_awake_cb,
> which provides "several thousands unreliable messages" to be queued.
> If you need greater control, you can implement your own by using a
> custom queue and awake() solely to interrupt Fl::run().
>
> There's a new chapter in the documentation ("Advanced FLTK") which
> discuss those functions in more detail.

Thanks for your advice. Here's what I wound up doing...

The main process handles all the updates to the GUI. No FLTK calls are made 
outside of this thread. The things I added to the main process follow:

queue<char*> messageQueue;
pthread_mutex_t messageMutex = PTHREAD_MUTEX_INITIALIZER;

void add_message(char* message) {
  pthread_mutex_lock(&messageMutex);
  messageQueue.push(message);
  pthread_mutex_unlock(&messageMutex);
}

void process_message(char* message) {
  // Manipulate GUI stuff based on message
}

Instead of using Fl::run(), I use this:

while (true) {
  Fl::wait();
  pthread_mutex_lock(&messageMutex);
  while (messageQueue.empty() == false) {
    char* message = messageQueue.front();
    messageQueue.pop();
    process_message(message);
    delete [] message;
  }
  pthread_mutex_unlock(&messageMutex);
}

The thread that does socket communication has a pointer to the add_message 
function. When it receives a message from a socket it calls add_message with 
the message.

Nicholas
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to