You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.4KB

  1. /**
  2. \page advanced Advanced FLTK
  3. This chapter explains advanced programming and design topics
  4. that will help you to get the most out of FLTK.
  5. \section advanced_multithreading Multithreading
  6. FLTK supports multithreaded applications using a locking mechanism
  7. based on "pthreads". We do not provide a threading interface as part of
  8. the library. However a simple example how threads can be implemented
  9. for all supported platforms can be found in \p test/threads.h
  10. and \p test/threads.cxx.
  11. To use the locking mechanism, FLTK must be compiled with
  12. \p --enable-threads set during the \p configure
  13. process. IDE-based versions of FLTK are automatically compiled with
  14. locking enabled if possible.
  15. In \p main(), call
  16. Fl::lock() before
  17. Fl::run() or
  18. Fl::wait() to start the runtime
  19. multithreading support for your program. All callbacks and derived
  20. functions like \p handle() and \p draw() will now be properly
  21. locked:
  22. \code
  23. int main() {
  24. Fl::lock();
  25. /* run thread */
  26. while (Fl::wait() > 0) {
  27. if (Fl::thread_message()) {
  28. /* process your data */
  29. }
  30. }
  31. }
  32. \endcode
  33. You can now start as many threads as you like. From within
  34. a thread (other than the main thread) FLTK calls must be wrapped
  35. with calls to Fl::lock() and Fl::unlock():
  36. \code
  37. Fl::lock(); // avoid conflicting calls
  38. ... // your code here
  39. Fl::unlock(); // allow other threads to access FLTK again
  40. \endcode
  41. You can send messages from child threads to the main thread
  42. using Fl::awake(void* message):
  43. \code
  44. void *msg; // "msg" is a pointer to your message
  45. Fl::awake(msg); // send "msg" to main thread
  46. \endcode
  47. A message can be anything you like. The main thread can retrieve
  48. the message by calling Fl::thread_message(). See example above.
  49. You can also tell the main thread to call a function for you
  50. as soon as possible by using
  51. Fl::awake(Fl_Awake_Handler cb, void* userdata):
  52. \code
  53. void do_something(void *userdata) {
  54. // running with the main thread
  55. }
  56. // running in another thread
  57. void *data; // "data" is a pointer to your user data
  58. Fl::awake(do_something, data); // call something in main thread
  59. \endcode
  60. FLTK supports multiple platforms, some of which allow only the
  61. main thread to handle system events and open or close windows.
  62. The safe thing to do is to adhere to the following rules for
  63. threads on all operating systems:
  64. \li Don't \p show() or \p hide() anything that contains
  65. widgets derived from Fl_Window, including dialogs, file
  66. choosers, subwindows or those using Fl_Gl_Window.
  67. \li Don't call Fl::wait(), Fl::flush() or any
  68. related methods that will handle system messages
  69. \li Don't start or cancel timers
  70. \li Don't change window decorations or titles
  71. \li The \p make_current() method may or may not work well for
  72. regular windows, but should always work for a Fl_Gl_Window
  73. to allow for high speed rendering on graphics cards with multiple
  74. pipelines
  75. See also:
  76. Fl::awake(void* message),
  77. Fl::lock(),
  78. Fl::thread_message(),
  79. Fl::unlock().
  80. \htmlonly
  81. <hr>
  82. <table summary="navigation bar" width="100%" border="0">
  83. <tr>
  84. <td width="45%" align="LEFT">
  85. <a class="el" href="fluid.html">
  86. [Prev]
  87. Programming with FLUID
  88. </a>
  89. </td>
  90. <td width="10%" align="CENTER">
  91. <a class="el" href="index.html">[Index]</a>
  92. </td>
  93. <td width="45%" align="RIGHT">
  94. <a class="el" href="unicode.html">
  95. Unicode and utf-8 Support
  96. [Next]
  97. </a>
  98. </td>
  99. </tr>
  100. </table>
  101. \endhtmlonly
  102. */