2007-01-19 01:48:19 +03:00
< HTML >
< HEAD >
< TITLE > 10 - Advanced FLTK< / TITLE >
< / HEAD >
< BODY >
< H1 ALIGN = "RIGHT" > < A NAME = "advanced" > 10 - Advanced FLTK< / A > < / H1 >
< P > This chapter explains advanced programming and design topics
that will help you to get the most out of FLTK.< / P >
2007-02-09 02:04:20 +03:00
< H2 > < A NAME = "multithreading" > Multithreading< / H2 >
2007-01-19 01:48:19 +03:00
2007-02-09 02:04:20 +03:00
< P > FLTK supports multithreaded application 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 < tt > test/threads.h< / tt > and < tt > test/threads.cxx< / tt > .
2007-01-19 01:48:19 +03:00
2007-02-09 02:04:20 +03:00
< P > To use the locking mechanism, FLTK must be compiled with < tt > --enable-threads< / tt > set during the < tt > configure< / tt > process. IDE-based versions of FLTK are automatically compiled with locking enabled if possible.
2007-01-19 01:48:19 +03:00
2007-02-12 19:00:28 +03:00
< P > In < TT > main()< / TT > , call < a href = "Fl.html#Fl.lock" > < TT > Fl::lock()< / TT > < / A > before < A HREF = "Fl.html#Fl.run" > < TT > Fl::run()< / TT > < / A > or < A HREF = "Fl.html#Fl.wait" > < TT > Fl::wait()< / TT > < / A > to start the runtime multithreading support for your program. All callbacks and derived functions like < tt > handle()< / tt > and < tt > draw()< / tt > will now be properly locked:< / P >
2007-01-19 01:48:19 +03:00
< pre >
2007-02-12 19:00:28 +03:00
int main() {
Fl::lock();
/* run thread */
2007-02-20 21:39:45 +03:00
while (Fl::wait() > 0) {
2007-02-20 20:02:41 +03:00
if (Fl::thread_message()) {
2007-02-12 19:00:28 +03:00
/* process your data */
2007-02-20 20:02:41 +03:00
}
2007-01-19 01:48:19 +03:00
}
}
< / pre >
< P > You can now start as many threads as you like. From within
a thread (other than the main thread) FLTK calls must be wrapped
2007-03-20 13:41:14 +03:00
with calls to < a href = "Fl.html#Fl.lock" > < tt > Fl::lock()< / tt > < / a >
and < a href = "Fl.html#Fl.unlock" > < tt > Fl::unlock()< / tt > < / a > :
2007-01-19 01:48:19 +03:00
< pre >
2007-02-12 19:00:28 +03:00
Fl::lock(); // avoid conflicting calls
... // your code here
Fl::unlock(); // allow other threads to access FLTK again
< / pre >
2007-03-20 13:41:14 +03:00
< p > You can send messages from child threads to the main thread
using < a href = "Fl.html#Fl.awake" > < tt > Fl::awake(msg)< / tt > < / a > :< / p >
2007-01-19 01:48:19 +03:00
2007-02-12 19:00:28 +03:00
< pre >
void *msg; // "msg" is a pointer to your message
Fl::awake(msg); // send "msg" to main thread
2007-01-19 01:48:19 +03:00
< / pre >
2007-03-20 13:41:14 +03:00
< p > You can also tell the main thread to call a function for you
as soon as possible by using
< a href = "Fl.html#Fl.awake" > < tt > Fl::awake(callback, userdata)< / tt > < / a > :< / p >
2007-02-12 19:00:28 +03:00
< pre >
2007-03-20 13:41:14 +03:00
void do_something(void *userdata) {
// running with the main thread
2007-02-12 19:00:28 +03:00
}
2007-03-20 13:41:14 +03:00
// running in another thread
void *data; // "data" is a pointer to your user data
Fl::awake(do_something, data); // call something in main thread
2007-02-12 19:00:28 +03:00
< / pre >
2007-03-20 13:41:14 +03:00
2007-02-12 19:00:28 +03:00
2007-01-19 01:48:19 +03:00
< P > FLTK supports multiple platforms, some of them which do not
allow any other but the main thread to handle system events and
open or close windows. The safe thing to do is to adhere to the
2007-02-09 02:04:20 +03:00
following rules for threads on all operating systems:
2007-01-19 01:48:19 +03:00
< ul >
2007-02-09 02:04:20 +03:00
< li > Don't < tt > show()< / tt > or < tt > hide()< / tt > anything that contains
2007-01-19 01:48:19 +03:00
widgets derived from < tt > Fl_Window< / tt > , including dialogs, file
choosers, subwindows or < tt > Fl_GL_Window< / tt > s< / li >
2007-02-09 02:04:20 +03:00
< li > Don't call < tt > Fl::wait()< / tt > , < tt > Fl::flush()< / tt > or any
2007-01-19 01:48:19 +03:00
related methods that will handle system messages< / li >
2007-02-09 02:04:20 +03:00
< li > Don't start or cancel timers< / li >
2007-01-19 01:48:19 +03:00
2007-02-09 02:04:20 +03:00
< li > Don't change window decorations or titles< / li >
2007-01-19 01:48:19 +03:00
2007-02-09 02:04:20 +03:00
< li > The < tt > make_current()< / tt > method may or may not work well for regular windows, but should always work for < tt > Fl_GL_Window< / tt > s to allow for high speed rendering on graphics cards with multiple pipelines< / li >
2007-01-19 01:48:19 +03:00
< / ul >
< P > See also:
2007-02-09 02:04:20 +03:00
< a href = "Fl.html#Fl.awake" > void awake(void *message)< / A > ,
2007-01-19 01:48:19 +03:00
< a href = "Fl.html#Fl.lock" > void lock()< / A > ,
2007-02-12 19:00:28 +03:00
< a href = "Fl.html#Fl.thread_message" > void *thread_message()< / A > ,
< a href = "Fl.html#Fl.unlock" > void unlock()< / A > .
2007-01-19 01:48:19 +03:00
< / BODY >
< / HTML >