From b5d637a4cdfa54a1c8843f03733f0b91fb9c4f12 Mon Sep 17 00:00:00 2001
From: Michael R Sweet
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() to start the runtime multithreading support for your program. All callbacks and derived functions like handle() and draw() will now be properly locked. +
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:
- main() {
- Fl::lock();
- /* run thread */
- while(Fl::wait() > 0) {
- if(Fl::thread_message()) {
- /* process your data */
+ int main() {
+ Fl::lock();
+ /* run thread */
+ while (Fl::wait() > 0) {
+ if (Fl::thread_message()) {
+ /* process your data */
+ }
}
}
- }
-
You can now start as many threads as you like. From within a thread (other than the main thread) FLTK calls must be wrapped -in the following code: +with calls to Fl::lock() and Fl::unlock():
- Fl::lock(); // avoid conflicting calls - ... // your code here - Fl::unlock(); // allow other threads to access FLTK again - Fl::awake(msg); // tells FLTK that another thread has made changes + Fl::lock(); // avoid conflicting calls + ... // your code here + Fl::unlock(); // allow other threads to access FLTK again ++ +
You can send messages from child threads to the main thread using Fl::awake(msg):
++ void *msg; // "msg" is a pointer to your message + Fl::awake(msg); // send "msg" to main thread+
These messages can be read by the main thread using Fl::thread_message() or by registering a message callback with Fl::set_awake_cb():
+ +
+ void message_cb(void *msg) {
+ ... do something with "msg" ...
+ }
+
+ int main() {
+ Fl::lock();
+ Fl::set_awake_cb(message_cb);
+ /* run thread */
+ return (Fl::run());
+ }
+
+
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 @@ -67,9 +86,9 @@ related methods that will handle system messages
See also: void awake(void *message), void lock(), -void unlock(), void set_awake_cb(void (*cb)(void *), -void *thread_message(). +void *thread_message(), +void unlock().