> > The question: what is the correct way to fire a graphics update from
> a thread ?




> Others will probably chime in and tell you more about programming with
> threads and FLTK.

Ah... That quite possibly means me then...

OK, at a high level, it goes something like this:

Firstly, a caveat: Rendering to the display device from any thread other than 
the "main" thread is not robust (may work on some systems, will fail in 
horrible, hard to debug ways, on others, so do not do that.)

Instead, all drawing is done from the "main" thread. In particular, note that 
creating/deleting/hiding/showing windows *has* to be done by the "main" thread.

However, if you create your widgets in the "main" thread, you can alter them 
from other threads quite safely, if you use the fltk locking methods. You can 
signal updates between threads using Fl::awake(); there's no need to get fancy 
with pipe's and so forth (fltk is handling that internally) and it is more 
portable.

Here's a simple recipe for this:

- in the "main" thread, before you create any windows, call Fl::lock(); *just 
once* to enable thread support. (Do not do any more locking/unlocking in the 
main thread, let fltk manage that for you.)

- in a worker thread, if you want to update a widget, do:

   Fl::lock();
   my_widget->change_this();
   my_other_widget->change_that(value);
   etc.;
   Fl::unlock();
   Fl::awake();

And that should update the widgets in a thread-safe way, then flag the main 
thread to manage the redraw sequence.

Note that Fl::awake can also be passed a callback function - the "main" thread 
will then execute that callback in its context, allowing the worker threads to 
do some operations that would not otherwise be allowed (e.g. you may be able to 
use this to create new windows under the control of a child thread, since the 
actual creation will occur in the context of the "main" thread.)

However, that is often not necessary, the simple case I outlined above covers a 
lot of use cases.



Selex ES Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 
3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to