FLTK 1.3.3
|
This chapter explains advanced programming and design topics that will help you to get the most out of FLTK.
FLTK supports multithreaded applications using a locking mechanism based on "pthreads". We do not provide a threading interface as part of the library. However a simple example how threads can be implemented for all supported platforms can be found in test/threads.h
and test/threads.cxx
.
To use the locking mechanism, FLTK must be compiled with –enable-threads
set during the configure
process. IDE-based versions of FLTK are automatically compiled with locking enabled if possible.
In main()
, call Fl::lock() before Fl::run() or Fl::wait() to start the runtime multithreading support for your program. All callbacks and derived functions like handle()
and draw()
will now be properly locked:
You can now start as many threads as you like. From within a thread (other than the main thread) FLTK calls must be wrapped with calls to Fl::lock() and Fl::unlock():
You can send messages from child threads to the main thread using Fl::awake(void* message):
A message can be anything you like. The main thread can retrieve the message by calling Fl::thread_message(). See example above.
You can also tell the main thread to call a function for you as soon as possible by using Fl::awake(Fl_Awake_Handler cb, void* userdata):
FLTK supports multiple platforms, some of which allow only the main thread to handle system events and open or close windows. The safe thing to do is to adhere to the following rules for threads on all operating systems:
show()
or hide()
anything that contains widgets derived from Fl_Window, including dialogs, file choosers, subwindows or those using Fl_Gl_Window.make_current()
method may or may not work well for regular windows, but should always work for a Fl_Gl_Window to allow for high speed rendering on graphics cards with multiple pipelinesSee also: Fl::awake(void* message), Fl::lock(), Fl::thread_message(), Fl::unlock().
[Prev] Programming with FLUID | [Index] | Unicode and utf-8 Support [Next] |