ntk-chtheme: Add new color scheme. Save/restore selection color.
[ntk.git] / test / threads.cxx
blobabaf1d520ffb48c110cae7cc3e1de9b61e7c8d5c
1 //
2 // "$Id: threads.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $"
3 //
4 // Threading example program for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
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
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include <config.h>
30 #if HAVE_PTHREAD || defined(WIN32)
31 # include <FL/Fl.H>
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>
36 # include "threads.h"
37 # include <stdio.h>
38 # include <math.h>
40 Fl_Thread prime_thread;
42 Fl_Browser *browser1, *browser2;
43 Fl_Value_Output *value1, *value2;
44 int start2 = 3;
46 void magic_number_cb(void *p)
48 Fl_Value_Output *w = (Fl_Value_Output*)p;
49 w->labelcolor(FL_RED);
50 w->redraw_label();
53 void* prime_func(void* p)
55 Fl_Browser* browser = (Fl_Browser*) p;
56 Fl_Value_Output *value;
57 int n;
58 int step;
59 char proud = 0;
61 if (browser == browser2) {
62 n = start2;
63 start2 += 2;
64 step = 12;
65 value = value2;
66 } else {
67 n = 3;
68 step = 2;
69 value = value1;
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.
80 if (n>=0) for (;;) {
81 int pp;
82 int hn = (int)sqrt((double)n);
84 for (pp=3; pp<=hn; pp+=2) if ( n%pp == 0 ) break;
85 if (pp >= hn) {
86 char s[128];
87 sprintf(s, "%d", n);
89 // Obtain a lock before we access the browser widget...
90 Fl::lock();
92 browser->add(s);
93 browser->bottomline(browser->size());
94 if (n > value->value()) value->value(n);
95 n += step;
97 // Release the lock...
98 Fl::unlock();
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
103 // just pass NULL.
104 Fl::awake(p);
105 if (n>10000 && !proud) {
106 proud = 1;
107 Fl::awake(magic_number_cb, value);
109 } else {
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.
113 Fl::lock();
114 n += step;
115 Fl::unlock();
118 return 0L;
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:");
127 w->end();
128 w->show(argc, argv);
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:");
133 w->end();
134 w->show();
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...
143 Fl::lock();
145 // Start threads...
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);
158 Fl::run();
160 return 0;
162 #else
163 # include <FL/fl_ask.H>
165 int main() {
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 $".