2001-12-08 19:23:51 +03:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
2001-12-08 19:23:51 +03:00
|
|
|
//
|
|
|
|
// Threading example program for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2006-01-15 05:26:54 +03:00
|
|
|
// Copyright 1998-2006 by Bill Spitzak and others.
|
2001-12-08 19:23:51 +03:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2001-12-08 19:23:51 +03:00
|
|
|
//
|
|
|
|
|
2005-04-16 04:01:49 +04:00
|
|
|
#include "config.h"
|
2001-12-08 19:23:51 +03:00
|
|
|
|
|
|
|
#if HAVE_PTHREAD || defined(WIN32)
|
|
|
|
# include <FL/Fl.H>
|
|
|
|
# include <FL/Fl_Window.H>
|
|
|
|
# include <FL/Fl_Browser.H>
|
|
|
|
# include <FL/Fl_Value_Output.H>
|
2007-03-09 19:33:13 +03:00
|
|
|
# include <FL/fl_ask.H>
|
2001-12-08 19:23:51 +03:00
|
|
|
# include "threads.h"
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <math.h>
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
Fl_Thread prime_thread;
|
|
|
|
|
|
|
|
Fl_Browser *browser1, *browser2;
|
2001-12-08 19:23:51 +03:00
|
|
|
Fl_Value_Output *value1, *value2;
|
|
|
|
int start2 = 3;
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2007-03-06 20:15:03 +03:00
|
|
|
void magic_number_cb(void *p)
|
|
|
|
{
|
|
|
|
Fl_Value_Output *w = (Fl_Value_Output*)p;
|
|
|
|
w->labelcolor(FL_RED);
|
|
|
|
w->redraw_label();
|
|
|
|
}
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
void* prime_func(void* p)
|
|
|
|
{
|
|
|
|
Fl_Browser* browser = (Fl_Browser*) p;
|
2001-12-08 19:23:51 +03:00
|
|
|
Fl_Value_Output *value;
|
|
|
|
int n;
|
|
|
|
int step;
|
2007-03-06 20:15:03 +03:00
|
|
|
char proud = 0;
|
2001-12-08 19:23:51 +03:00
|
|
|
|
|
|
|
if (browser == browser2) {
|
|
|
|
n = start2;
|
|
|
|
start2 += 2;
|
|
|
|
step = 12;
|
|
|
|
value = value2;
|
|
|
|
} else {
|
|
|
|
n = 3;
|
|
|
|
step = 2;
|
|
|
|
value = value1;
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2001-12-08 19:23:51 +03:00
|
|
|
// very simple prime number calculator !
|
2007-05-20 20:32:37 +04:00
|
|
|
//
|
|
|
|
// The return at the end of this function can never be reached and thus
|
|
|
|
// will generate a warning with some compilers, however we need to have
|
|
|
|
// a return statement or other compilers will complain there is no return
|
|
|
|
// statement. To avoid warnings on all compilers, we fool the smart ones
|
|
|
|
// into beleiving that there is a chance that we reach the end by testing
|
|
|
|
// n>=0, knowing that logically, n will never be negative in this context.
|
|
|
|
if (n>=0) for (;;) {
|
2007-02-12 19:00:28 +03:00
|
|
|
int pp;
|
2004-04-08 00:06:17 +04:00
|
|
|
int hn = (int)sqrt((double)n);
|
2006-01-06 19:53:04 +03:00
|
|
|
|
2007-02-12 19:00:28 +03:00
|
|
|
for (pp=3; pp<=hn; pp+=2) if ( n%pp == 0 ) break;
|
|
|
|
if (pp >= hn) {
|
2001-08-02 01:24:49 +04:00
|
|
|
char s[128];
|
|
|
|
sprintf(s, "%d", n);
|
2007-02-12 19:00:28 +03:00
|
|
|
|
|
|
|
// Obtain a lock before we access the browser widget...
|
2001-08-02 01:24:49 +04:00
|
|
|
Fl::lock();
|
2007-02-12 19:00:28 +03:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
browser->add(s);
|
2001-12-08 19:23:51 +03:00
|
|
|
browser->bottomline(browser->size());
|
|
|
|
if (n > value->value()) value->value(n);
|
2006-01-06 19:53:04 +03:00
|
|
|
n += step;
|
2007-02-12 19:00:28 +03:00
|
|
|
|
|
|
|
// Release the lock...
|
2001-08-02 01:24:49 +04:00
|
|
|
Fl::unlock();
|
2007-02-12 19:00:28 +03:00
|
|
|
|
|
|
|
// Send a message to the main thread, at which point it will
|
|
|
|
// process any pending redraws for our browser widget. The
|
|
|
|
// message we pass here isn't used for anything, so we could also
|
|
|
|
// just pass NULL.
|
|
|
|
Fl::awake(p);
|
2007-03-06 20:15:03 +03:00
|
|
|
if (n>10000 && !proud) {
|
|
|
|
proud = 1;
|
|
|
|
Fl::awake(magic_number_cb, value);
|
|
|
|
}
|
2006-01-06 19:53:04 +03:00
|
|
|
} else {
|
2007-02-12 19:00:28 +03:00
|
|
|
// This should not be necessary since "n" and "step" are local variables,
|
2006-01-06 19:53:04 +03:00
|
|
|
// however it appears that at least MacOS X has some threading issues
|
|
|
|
// that cause semi-random corruption of the (stack) variables.
|
|
|
|
Fl::lock();
|
|
|
|
n += step;
|
|
|
|
Fl::unlock();
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-20 20:32:37 +04:00
|
|
|
return 0L;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
2001-12-23 06:40:51 +03:00
|
|
|
int main(int argc, char **argv)
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
2001-12-08 19:23:51 +03:00
|
|
|
Fl_Window* w = new Fl_Window(200, 200, "Single Thread");
|
|
|
|
browser1 = new Fl_Browser(0, 0, 200, 175);
|
|
|
|
w->resizable(browser1);
|
|
|
|
value1 = new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
|
2001-08-02 01:24:49 +04:00
|
|
|
w->end();
|
2001-12-23 06:40:51 +03:00
|
|
|
w->show(argc, argv);
|
2001-12-08 19:23:51 +03:00
|
|
|
w = new Fl_Window(200, 200, "Six Threads");
|
|
|
|
browser2 = new Fl_Browser(0, 0, 200, 175);
|
|
|
|
w->resizable(browser2);
|
|
|
|
value2 = new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
|
2001-08-02 01:24:49 +04:00
|
|
|
w->end();
|
|
|
|
w->show();
|
|
|
|
|
2001-12-08 19:23:51 +03:00
|
|
|
browser1->add("Prime numbers:");
|
|
|
|
browser2->add("Prime numbers:");
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2007-02-12 19:00:28 +03:00
|
|
|
// Enable multi-thread support by locking from the main
|
|
|
|
// thread. Fl::wait() and Fl::run() call Fl::unlock() and
|
|
|
|
// Fl::lock() as needed to release control to the child threads
|
|
|
|
// when it is safe to do so...
|
|
|
|
Fl::lock();
|
|
|
|
|
|
|
|
// Start threads...
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// One thread displaying in one browser
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser1);
|
2007-02-12 19:00:28 +03:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
// Several threads displaying in another browser
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser2);
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser2);
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser2);
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser2);
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser2);
|
|
|
|
fl_create_thread(prime_thread, prime_func, browser2);
|
|
|
|
|
2007-02-12 19:00:28 +03:00
|
|
|
Fl::run();
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2001-12-08 19:23:51 +03:00
|
|
|
#else
|
|
|
|
# include <FL/fl_ask.H>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
fl_alert("Sorry, threading not supported on this platform!");
|
|
|
|
}
|
|
|
|
#endif // HAVE_PTHREAD || WIN32
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
2001-12-08 19:23:51 +03:00
|
|
|
//
|