2 // "$Id: threads.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $"
4 // Threading example program for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
30 #if HAVE_PTHREAD || defined(WIN32)
32 # include <FL/Fl_Double_Window.H>
33 # include <FL/Fl_Browser.H>
34 # include <FL/Fl_Value_Output.H>
35 # include <FL/fl_ask.H>
40 Fl_Thread prime_thread
;
42 Fl_Browser
*browser1
, *browser2
;
43 Fl_Value_Output
*value1
, *value2
;
46 void magic_number_cb(void *p
)
48 Fl_Value_Output
*w
= (Fl_Value_Output
*)p
;
49 w
->labelcolor(FL_RED
);
53 void* prime_func(void* p
)
55 Fl_Browser
* browser
= (Fl_Browser
*) p
;
56 Fl_Value_Output
*value
;
61 if (browser
== browser2
) {
72 // very simple prime number calculator !
74 // The return at the end of this function can never be reached and thus
75 // will generate a warning with some compilers, however we need to have
76 // a return statement or other compilers will complain there is no return
77 // statement. To avoid warnings on all compilers, we fool the smart ones
78 // into beleiving that there is a chance that we reach the end by testing
79 // n>=0, knowing that logically, n will never be negative in this context.
82 int hn
= (int)sqrt((double)n
);
84 for (pp
=3; pp
<=hn
; pp
+=2) if ( n
%pp
== 0 ) break;
89 // Obtain a lock before we access the browser widget...
93 browser
->bottomline(browser
->size());
94 if (n
> value
->value()) value
->value(n
);
97 // Release the lock...
100 // Send a message to the main thread, at which point it will
101 // process any pending redraws for our browser widget. The
102 // message we pass here isn't used for anything, so we could also
105 if (n
>10000 && !proud
) {
107 Fl::awake(magic_number_cb
, value
);
110 // This should not be necessary since "n" and "step" are local variables,
111 // however it appears that at least MacOS X has some threading issues
112 // that cause semi-random corruption of the (stack) variables.
121 int main(int argc
, char **argv
)
123 Fl_Double_Window
* w
= new Fl_Double_Window(200, 200, "Single Thread");
124 browser1
= new Fl_Browser(0, 0, 200, 175);
125 w
->resizable(browser1
);
126 value1
= new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
129 w
= new Fl_Double_Window(200, 200, "Six Threads");
130 browser2
= new Fl_Browser(0, 0, 200, 175);
131 w
->resizable(browser2
);
132 value2
= new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
136 browser1
->add("Prime numbers:");
137 browser2
->add("Prime numbers:");
139 // Enable multi-thread support by locking from the main
140 // thread. Fl::wait() and Fl::run() call Fl::unlock() and
141 // Fl::lock() as needed to release control to the child threads
142 // when it is safe to do so...
147 // One thread displaying in one browser
148 fl_create_thread(prime_thread
, prime_func
, browser1
);
150 // Several threads displaying in another browser
151 fl_create_thread(prime_thread
, prime_func
, browser2
);
152 fl_create_thread(prime_thread
, prime_func
, browser2
);
153 fl_create_thread(prime_thread
, prime_func
, browser2
);
154 fl_create_thread(prime_thread
, prime_func
, browser2
);
155 fl_create_thread(prime_thread
, prime_func
, browser2
);
156 fl_create_thread(prime_thread
, prime_func
, browser2
);
163 # include <FL/fl_ask.H>
166 fl_alert("Sorry, threading not supported on this platform!");
168 #endif // HAVE_PTHREAD || WIN32
172 // End of "$Id: threads.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $".