Fix threading test.
Add threading demo from 2.0, with minor improvements to make the prime number generation faster/more realistic. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1823 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
a93a8031f1
commit
64f2cc0635
11
configh.in
11
configh.in
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* "$Id: configh.in,v 1.11.2.11.2.4 2001/10/19 14:04:08 easysw Exp $"
|
||||
* "$Id: configh.in,v 1.11.2.11.2.5 2001/12/08 16:23:51 easysw Exp $"
|
||||
*
|
||||
* Configuration file for the Fast Light Tool Kit (FLTK).
|
||||
* @configure_input@
|
||||
@ -201,5 +201,12 @@
|
||||
|
||||
|
||||
/*
|
||||
* End of "$Id: configh.in,v 1.11.2.11.2.4 2001/10/19 14:04:08 easysw Exp $".
|
||||
* Do we have POSIX threading?
|
||||
*/
|
||||
|
||||
#undef HAVE_PTHREAD
|
||||
|
||||
|
||||
/*
|
||||
* End of "$Id: configh.in,v 1.11.2.11.2.5 2001/12/08 16:23:51 easysw Exp $".
|
||||
*/
|
||||
|
@ -1,7 +1,7 @@
|
||||
dnl -*- sh -*-
|
||||
dnl the "configure" script is made from this by running GNU "autoconf"
|
||||
dnl
|
||||
dnl "$Id: configure.in,v 1.33.2.31.2.37 2001/12/06 22:16:48 easysw Exp $"
|
||||
dnl "$Id: configure.in,v 1.33.2.31.2.38 2001/12/08 16:23:51 easysw Exp $"
|
||||
dnl
|
||||
dnl Configuration script for the Fast Light Tool Kit (FLTK).
|
||||
dnl
|
||||
@ -313,7 +313,7 @@ case $uname in
|
||||
AC_SEARCH_LIBS(pthread_create, pthread)
|
||||
fi
|
||||
|
||||
if test "x$ac_cv_search_pthread" != x; then
|
||||
if test "x$ac_cv_search_pthread_create" != x; then
|
||||
AC_DEFINE(HAVE_PTHREAD)
|
||||
fi
|
||||
|
||||
@ -604,5 +604,5 @@ AC_OUTPUT(makeinclude fltk.list fltk-config FL/Makefile)
|
||||
chmod +x fltk-config
|
||||
|
||||
dnl
|
||||
dnl End of "$Id: configure.in,v 1.33.2.31.2.37 2001/12/06 22:16:48 easysw Exp $".
|
||||
dnl End of "$Id: configure.in,v 1.33.2.31.2.38 2001/12/08 16:23:51 easysw Exp $".
|
||||
dnl
|
||||
|
@ -1,5 +1,5 @@
|
||||
#
|
||||
# "$Id: Makefile,v 1.19.2.7.2.16 2001/12/06 02:20:36 matthiaswm Exp $"
|
||||
# "$Id: Makefile,v 1.19.2.7.2.17 2001/12/08 16:23:51 easysw Exp $"
|
||||
#
|
||||
# Test/example program makefile for the Fast Light Tool Kit (FLTK).
|
||||
#
|
||||
@ -83,6 +83,7 @@ CPPFILES =\
|
||||
subwindow.cxx \
|
||||
symbols.cxx \
|
||||
tabs.cxx \
|
||||
threads.cxx \
|
||||
tile.cxx \
|
||||
tiled_image.cxx \
|
||||
valuators.cxx
|
||||
@ -135,6 +136,7 @@ ALL = \
|
||||
subwindow$(EXEEXT) \
|
||||
symbols$(EXEEXT) \
|
||||
tabs$(EXEEXT) \
|
||||
threads$(EXEEXT) \
|
||||
tile$(EXEEXT) \
|
||||
tiled_image$(EXEEXT) \
|
||||
valuators$(EXEEXT)
|
||||
@ -245,5 +247,5 @@ uninstall:
|
||||
@echo Nothing to uninstall in test directory.
|
||||
|
||||
#
|
||||
# End of "$Id: Makefile,v 1.19.2.7.2.16 2001/12/06 02:20:36 matthiaswm Exp $".
|
||||
# End of "$Id: Makefile,v 1.19.2.7.2.17 2001/12/08 16:23:51 easysw Exp $".
|
||||
#
|
||||
|
@ -62,11 +62,12 @@
|
||||
@e:Checkers:checkers
|
||||
|
||||
@main:Other\nTests:@o
|
||||
@o:color choosers:color_chooser r
|
||||
@o:file chooser:file_chooser
|
||||
@o:Color Choosers:color_chooser r
|
||||
@o:File Chooser:file_chooser
|
||||
@o:XForms Emulation:forms
|
||||
@o:fonts:fonts
|
||||
@o:Fonts:fonts
|
||||
@o:HelpDialog:help
|
||||
@o:Threading:threads
|
||||
|
||||
@main:Tutorial\nfrom\nManual:@j
|
||||
@j:ask\n(modified):ask
|
||||
|
@ -1,26 +1,75 @@
|
||||
#include <fltk/Fl.h>
|
||||
#include <fltk/Fl_Window.h>
|
||||
#include <fltk/Fl_Browser.h>
|
||||
#include <fltk/Fl_Threads.h>
|
||||
#include <stdio.h>
|
||||
//
|
||||
// "$Id: threads.cxx,v 1.10.2.1 2001/12/08 16:23:51 easysw Exp $"
|
||||
//
|
||||
// Threading example program for the Fast Light Tool Kit (FLTK).
|
||||
//
|
||||
// Copyright 1998-2001 by Bill Spitzak and others.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Please report all bugs and problems to "fltk-bugs@fltk.org".
|
||||
//
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#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>
|
||||
# include "threads.h"
|
||||
# include <stdio.h>
|
||||
# include <math.h>
|
||||
|
||||
Fl_Thread prime_thread;
|
||||
|
||||
Fl_Browser *browser1, *browser2;
|
||||
Fl_Value_Output *value1, *value2;
|
||||
int start2 = 3;
|
||||
|
||||
void* prime_func(void* p)
|
||||
{
|
||||
Fl_Browser* browser = (Fl_Browser*) p;
|
||||
Fl_Value_Output *value;
|
||||
int n;
|
||||
int step;
|
||||
|
||||
// very loosy prime number calculator !
|
||||
for (int n=1000000; ; n++) {
|
||||
if (browser == browser2) {
|
||||
n = start2;
|
||||
start2 += 2;
|
||||
step = 12;
|
||||
value = value2;
|
||||
} else {
|
||||
n = 3;
|
||||
step = 2;
|
||||
value = value1;
|
||||
}
|
||||
|
||||
// very simple prime number calculator !
|
||||
for (; ; n+= step) {
|
||||
int p;
|
||||
for (p=2; p<n; p++) if ( n%p == 0 ) break;
|
||||
if (p == n) {
|
||||
int hn = (int)sqrt(n);
|
||||
for (p=3; p<=hn; p+=2) if ( n%p == 0 ) break;
|
||||
if (p >= hn) {
|
||||
char s[128];
|
||||
sprintf(s, "%d", n);
|
||||
Fl::lock();
|
||||
browser->add(s);
|
||||
browser->bottomline(browser->size());
|
||||
if (n > value->value()) value->value(n);
|
||||
Fl::unlock();
|
||||
Fl::awake((void*) (browser == browser1? p:0)); // Cause the browser to redraw ...
|
||||
}
|
||||
@ -30,17 +79,21 @@ void* prime_func(void* p)
|
||||
|
||||
int main()
|
||||
{
|
||||
Fl_Window* w = new Fl_Window(200, 300, "Multithread test");
|
||||
browser1 = new Fl_Browser(0, 0, 200, 300);
|
||||
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:");
|
||||
w->end();
|
||||
w->show();
|
||||
w = new Fl_Window(200, 300, "Multithread test");
|
||||
browser2 = new Fl_Browser(0, 0, 200, 300);
|
||||
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:");
|
||||
w->end();
|
||||
w->show();
|
||||
|
||||
browser1->add("Prime numbers :");
|
||||
browser2->add("Prime numbers :");
|
||||
browser1->add("Prime numbers:");
|
||||
browser2->add("Prime numbers:");
|
||||
|
||||
Fl::lock(); // you must do this before creating any threads!
|
||||
|
||||
@ -57,9 +110,21 @@ int main()
|
||||
// Fl::run();
|
||||
while (w->visible()) {
|
||||
Fl::wait();
|
||||
void* m = Fl::thread_message();
|
||||
if (m) printf("Recieved message: %d\n", int(m));
|
||||
// void* m = Fl::thread_message();
|
||||
// printf("Received message: %p\n", m);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
# include <FL/fl_ask.H>
|
||||
|
||||
int main() {
|
||||
fl_alert("Sorry, threading not supported on this platform!");
|
||||
}
|
||||
#endif // HAVE_PTHREAD || WIN32
|
||||
|
||||
|
||||
//
|
||||
// End of "$Id: threads.cxx,v 1.10.2.1 2001/12/08 16:23:51 easysw Exp $".
|
||||
//
|
||||
|
164
test/threads.h
Normal file
164
test/threads.h
Normal file
@ -0,0 +1,164 @@
|
||||
//
|
||||
// "$Id: threads.h,v 1.1.2.1 2001/12/08 16:23:51 easysw Exp $"
|
||||
//
|
||||
// Simple threading API for the Fast Light Tool Kit (FLTK).
|
||||
//
|
||||
// Copyright 1998-2001 by Bill Spitzak and others.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Please report all bugs and problems to "fltk-bugs@fltk.org".
|
||||
//
|
||||
|
||||
// Inline classes to provide portable support for threads and mutexes.
|
||||
//
|
||||
// FLTK does not use this (it has an internal mutex implementation
|
||||
// that is used if Fl::lock() is called). This header file's only
|
||||
// purpose is so we can write portable demo programs. It may be useful
|
||||
// or an inspiration to people who want to try writing multithreaded
|
||||
// programs themselves.
|
||||
//
|
||||
// FLTK has no multithreaded support unless the main thread calls Fl::lock().
|
||||
// This main thread is the only thread allowed to call Fl::run() or Fl::wait().
|
||||
// From then on FLTK will be locked except when the main thread is actually
|
||||
// waiting for events from the user. Other threads must call Fl::lock() and
|
||||
// Fl::unlock() to surround calls to FLTK (such as to change widgets or
|
||||
// redraw them).
|
||||
|
||||
#ifndef Threads_H
|
||||
# define Threads_H
|
||||
|
||||
# ifndef WIN32
|
||||
// Use POSIX threading...
|
||||
|
||||
# include <pthread.h>
|
||||
|
||||
typedef pthread_t Fl_Thread;
|
||||
|
||||
static int fl_create_thread(Fl_Thread& t, void *(*f) (void *), void* p)
|
||||
{
|
||||
return pthread_create((pthread_t*)&t, 0, f, p);
|
||||
}
|
||||
|
||||
// Linux supports recursive locks, use them directly, with some cheating:
|
||||
# ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
|
||||
|
||||
extern pthread_mutexattr_t Fl_Mutex_attrib;
|
||||
|
||||
class Fl_Mutex {
|
||||
friend class Fl_SignalMutex;
|
||||
pthread_mutex_t mutex;
|
||||
Fl_Mutex(const Fl_Mutex&);
|
||||
Fl_Mutex& operator=(const Fl_Mutex&);
|
||||
public:
|
||||
Fl_Mutex() {pthread_mutex_init(&mutex, &Fl_Mutex_attrib);}
|
||||
void lock() {pthread_mutex_lock(&mutex);}
|
||||
void unlock() {pthread_mutex_unlock(&mutex);}
|
||||
~Fl_Mutex() {pthread_mutex_destroy(&mutex);}
|
||||
};
|
||||
|
||||
class Fl_SignalMutex : public Fl_Mutex {
|
||||
pthread_cond_t cond;
|
||||
public:
|
||||
Fl_SignalMutex() : Fl_Mutex() {pthread_cond_init(&cond, 0);}
|
||||
void signal() {pthread_cond_broadcast(&cond);}
|
||||
void wait() {
|
||||
int save_counter = mutex.m_count; mutex.m_count = 1;
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
mutex.m_count = save_counter;
|
||||
}
|
||||
};
|
||||
|
||||
# else // standard pthread mutexes need a bit of work to be recursive:
|
||||
|
||||
class Fl_Mutex {
|
||||
friend class Fl_SignalMutex;
|
||||
pthread_mutex_t mutex;
|
||||
pthread_t owner;
|
||||
int counter;
|
||||
Fl_Mutex(const Fl_Mutex&);
|
||||
Fl_Mutex& operator=(const Fl_Mutex&);
|
||||
public:
|
||||
Fl_Mutex() : counter(0) {pthread_mutex_init(&mutex, 0);}
|
||||
void lock() {
|
||||
if (!counter || owner != pthread_self()) {
|
||||
pthread_mutex_lock(&mutex); owner = pthread_self();
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
void unlock() {if (!--counter) pthread_mutex_unlock(&mutex);}
|
||||
~Fl_Mutex() {pthread_mutex_destroy(&mutex);}
|
||||
};
|
||||
|
||||
class Fl_SignalMutex : public Fl_Mutex {
|
||||
pthread_cond_t cond;
|
||||
public:
|
||||
Fl_SignalMutex() : Fl_Mutex() {pthread_cond_init(&cond, 0);}
|
||||
void signal() {pthread_cond_broadcast(&cond);}
|
||||
void wait() {
|
||||
int save_counter = counter; counter = 0;
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
counter = save_counter;
|
||||
owner = pthread_self();
|
||||
}
|
||||
};
|
||||
|
||||
# endif
|
||||
|
||||
# else // Use Windows threading...
|
||||
|
||||
# include <windows.h>
|
||||
# include <process.h>
|
||||
|
||||
typedef unsigned long Fl_Thread;
|
||||
|
||||
static int fl_create_thread(Fl_Thread& t, void *(*f) (void *), void* p) {
|
||||
return t = (Fl_Thread)_beginthread((void( __cdecl * )( void * ))f, 0, p);
|
||||
}
|
||||
|
||||
class FL_API Fl_Mutex {
|
||||
friend class Fl_SignalMutex;
|
||||
CRITICAL_SECTION cs;
|
||||
Fl_Mutex(const Fl_Mutex&);
|
||||
Fl_Mutex& operator=(const Fl_Mutex&);
|
||||
public:
|
||||
Fl_Mutex() {InitializeCriticalSection(&cs);}
|
||||
void lock() {EnterCriticalSection(&cs);}
|
||||
void unlock() {LeaveCriticalSection(&cs);}
|
||||
~Fl_Mutex() {DeleteCriticalSection(&cs);}
|
||||
};
|
||||
|
||||
class FL_API Fl_SignalMutex : public Fl_Mutex {
|
||||
HANDLE event;
|
||||
public:
|
||||
Fl_SignalMutex() : Fl_Mutex() {event = CreateEvent(0, FALSE, FALSE, 0);}
|
||||
void signal() {SetEvent(event);}
|
||||
void wait() {
|
||||
// int save_counter = cs.count; cs.count = 1;
|
||||
// the following three calls should be atomic, sigh...
|
||||
LeaveCriticalSection(&cs);
|
||||
WaitForSingleObject(event, INFINITE);
|
||||
EnterCriticalSection(&cs);
|
||||
// cs.count = save_counter;
|
||||
}
|
||||
};
|
||||
|
||||
# endif // !WIN32
|
||||
#endif // !Threads_h
|
||||
|
||||
//
|
||||
// End of "$Id: threads.h,v 1.1.2.1 2001/12/08 16:23:51 easysw Exp $".
|
||||
//
|
Loading…
Reference in New Issue
Block a user