Improve threads demo test/threads.cxx (#1263)
- Replace Fl_Browser with Fl_Terminal which uses a constant buffer size - Don't lock the GUI for every single prime. Collect primes for at least 0.25 seconds before calling Fl::awake(handler, buffer) - Use (two) alternate buffers for collecting prime data. - Use Fl::lock() *only* to protect thread data at initialization time. Observation on Debian 12, CPU: 12-core, 12th Gen Intel Core i7-1260P: speedup > factor 4, using multiple cores, GUI fully functional: scrolling the display, resizing, ... Tested natively (X11 + Wayland) and cross-compiled for Windows, using `wine`.
This commit is contained in:
parent
acd77fa8dc
commit
088d98389c
166
test/threads.cxx
166
test/threads.cxx
@ -1,7 +1,7 @@
|
|||||||
//
|
//
|
||||||
// Threading example program for the Fast Light Tool Kit (FLTK).
|
// Threading example program for the Fast Light Tool Kit (FLTK).
|
||||||
//
|
//
|
||||||
// Copyright 1998-2018 by Bill Spitzak and others.
|
// Copyright 1998-2025 by Bill Spitzak and others.
|
||||||
//
|
//
|
||||||
// This library is free software. Distribution and use rights are outlined in
|
// This library is free software. Distribution and use rights are outlined in
|
||||||
// the file "COPYING" which should have been included with this file. If this
|
// the file "COPYING" which should have been included with this file. If this
|
||||||
@ -19,77 +19,123 @@
|
|||||||
#if defined(HAVE_PTHREAD) || defined(_WIN32)
|
#if defined(HAVE_PTHREAD) || defined(_WIN32)
|
||||||
# include <FL/Fl.H>
|
# include <FL/Fl.H>
|
||||||
# include <FL/Fl_Double_Window.H>
|
# include <FL/Fl_Double_Window.H>
|
||||||
# include <FL/Fl_Browser.H>
|
# include <FL/Fl_Terminal.H>
|
||||||
# include <FL/Fl_Value_Output.H>
|
# include <FL/Fl_Value_Output.H>
|
||||||
# include <FL/fl_ask.H>
|
# include <FL/fl_ask.H>
|
||||||
# include "threads.h"
|
# include "threads.h"
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
# include <math.h>
|
# include <math.h>
|
||||||
|
# include <vector>
|
||||||
|
|
||||||
|
// min. time in seconds before calling Fl::awake(...)
|
||||||
|
#define DELTA 0.25
|
||||||
|
|
||||||
|
// struct to collect primes until at least <DELTA> seconds passed.
|
||||||
|
// Two such structs per thread are used as alternate buffers.
|
||||||
|
|
||||||
|
struct prime {
|
||||||
|
int idx; // thread index: 0 or {1..6}
|
||||||
|
int done; // set to 1 after it was worked on
|
||||||
|
Fl_Terminal *terminal; // widget to write output to
|
||||||
|
Fl_Value_Output *value; // highest prime
|
||||||
|
std::vector<unsigned long> primes; // collected primes within time frame
|
||||||
|
};
|
||||||
|
|
||||||
Fl_Thread prime_thread;
|
Fl_Thread prime_thread;
|
||||||
|
|
||||||
Fl_Browser *browser1, *browser2;
|
Fl_Terminal *tty1, *tty2;
|
||||||
Fl_Value_Output *value1, *value2;
|
Fl_Value_Output *value1, *value2;
|
||||||
int start2 = 3;
|
int start2 = 3;
|
||||||
|
|
||||||
void magic_number_cb(void *p)
|
void magic_number_cb(void *p) {
|
||||||
{
|
|
||||||
Fl_Value_Output *w = (Fl_Value_Output*)p;
|
Fl_Value_Output *w = (Fl_Value_Output*)p;
|
||||||
w->labelcolor(FL_RED);
|
w->labelcolor(FL_RED);
|
||||||
w->redraw_label();
|
w->redraw_label();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void* prime_func(void* p)
|
// This is called indirectly by Fl::awake(update_handler, (void *)prime)
|
||||||
{
|
// in the context of the main (FLTK GUI) thread.
|
||||||
Fl_Browser* browser = (Fl_Browser*) p;
|
|
||||||
|
void update_handler(void *v) {
|
||||||
|
struct prime *pr = (struct prime *)v;
|
||||||
|
for (auto n : pr->primes) {
|
||||||
|
pr->terminal->printf("prime: %10u\n", n);
|
||||||
|
if (n > pr->value->value())
|
||||||
|
pr->value->value(n);
|
||||||
|
}
|
||||||
|
pr->done = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void* prime_func(void* p) {
|
||||||
|
Fl_Terminal* terminal = (Fl_Terminal*)p;
|
||||||
Fl_Value_Output *value;
|
Fl_Value_Output *value;
|
||||||
int n;
|
unsigned long n;
|
||||||
int step;
|
int step;
|
||||||
char proud = 0;
|
char proud = 0;
|
||||||
|
|
||||||
if (browser == browser2) {
|
// initialize thread variables
|
||||||
n = start2;
|
|
||||||
|
if (terminal == tty2) { // multiple threads
|
||||||
|
Fl::lock(); // lock to prevent race condition on `start2`
|
||||||
|
n = start2;
|
||||||
start2 += 2;
|
start2 += 2;
|
||||||
step = 12;
|
Fl::unlock();
|
||||||
value = value2;
|
step = 12;
|
||||||
} else {
|
value = value2;
|
||||||
n = 3;
|
} else { // single thread
|
||||||
step = 2;
|
n = 3;
|
||||||
value = value1;
|
step = 2;
|
||||||
|
value = value1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initialize alternate buffers (struct prime) to store primes
|
||||||
|
|
||||||
|
struct prime pr[2];
|
||||||
|
pr[0].done = 0;
|
||||||
|
pr[1].done = 1;
|
||||||
|
pr[0].terminal = pr[1].terminal = terminal;
|
||||||
|
pr[0].idx = pr[1].idx = n/2 - 1;
|
||||||
|
pr[0].value = pr[1].value = value;
|
||||||
|
pr[0].primes.clear();
|
||||||
|
pr[1].primes.clear();
|
||||||
|
int pi = 0; // prime buffer index
|
||||||
|
|
||||||
|
Fl_Timestamp last = Fl::now();
|
||||||
|
|
||||||
// very simple prime number calculator !
|
// very simple prime number calculator !
|
||||||
//
|
//
|
||||||
// The return at the end of this function can never be reached and thus
|
// 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
|
// will generate a warning with some compilers, however we need to have
|
||||||
// a return statement or other compilers will complain there is no return
|
// a return statement or other compilers will complain there is no return
|
||||||
// statement. To avoid warnings on all compilers, we fool the smart ones
|
// statement. To avoid warnings on all compilers, we fool the smart ones into
|
||||||
// into beleiving that there is a chance that we reach the end by testing
|
// believing that there is a chance that we reach the end by testing n > 0,
|
||||||
// n>=0, knowing that logically, n will never be negative in this context.
|
// knowing that logically, n will never be less than 3 in this context.
|
||||||
if (n>=0) for (;;) {
|
|
||||||
|
if (n > 0) for (;;) {
|
||||||
int pp;
|
int pp;
|
||||||
int hn = (int)sqrt((double)n);
|
int hn = (int)sqrt((double)n);
|
||||||
|
|
||||||
for (pp=3; pp<=hn; pp+=2) if ( n%pp == 0 ) break;
|
for (pp = 3; pp <= hn; pp += 2) if ( n%pp == 0 ) break;
|
||||||
if (pp >= hn) {
|
|
||||||
char s[128];
|
|
||||||
snprintf(s, 128, "%d", n);
|
|
||||||
|
|
||||||
// Obtain a lock before we access the browser widget...
|
if (pp > hn) { // n is a prime
|
||||||
Fl::lock();
|
|
||||||
|
|
||||||
browser->add(s);
|
pr[pi].primes.push_back(n);
|
||||||
browser->bottomline(browser->size());
|
|
||||||
if (n > value->value()) value->value(n);
|
|
||||||
n += step;
|
|
||||||
|
|
||||||
// Release the lock...
|
|
||||||
Fl::unlock();
|
|
||||||
|
|
||||||
// Send a message to the main thread, at which point it will
|
// Send a message to the main thread, at which point it will
|
||||||
// process any pending redraws for our browser widget.
|
// process any pending updates.
|
||||||
Fl::awake();
|
|
||||||
if (n>10000 && !proud) {
|
double ssl = Fl::seconds_since(last);
|
||||||
|
|
||||||
|
if (ssl > DELTA && pr[1-pi].done) { // ready to switch buffers
|
||||||
|
last = Fl::now();
|
||||||
|
Fl::awake(update_handler, (void *)(&pr[pi]));
|
||||||
|
pi = 1 - pi; // switch to alternate buffer
|
||||||
|
pr[pi].primes.clear(); // clear primes
|
||||||
|
}
|
||||||
|
|
||||||
|
n += step;
|
||||||
|
|
||||||
|
if (n > 5*1000*1000 && !proud) {
|
||||||
proud = 1;
|
proud = 1;
|
||||||
Fl::awake(magic_number_cb, value);
|
Fl::awake(magic_number_cb, value);
|
||||||
}
|
}
|
||||||
@ -107,40 +153,44 @@ extern "C" void* prime_func(void* p)
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
Fl_Double_Window* w = new Fl_Double_Window(200, 200, "Single Thread");
|
// First window: single thread
|
||||||
browser1 = new Fl_Browser(0, 0, 200, 175);
|
Fl_Double_Window* w = new Fl_Double_Window(300, 200, "Single Thread");
|
||||||
w->resizable(browser1);
|
tty1 = new Fl_Terminal(0, 0, 300, 175);
|
||||||
|
w->resizable(tty1);
|
||||||
value1 = new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
|
value1 = new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
|
||||||
w->end();
|
w->end();
|
||||||
w->show(argc, argv);
|
w->show(argc, argv);
|
||||||
w = new Fl_Double_Window(200, 200, "Six Threads");
|
|
||||||
browser2 = new Fl_Browser(0, 0, 200, 175);
|
// Second window: multiple threads
|
||||||
w->resizable(browser2);
|
w = new Fl_Double_Window(300, 200, "Six Threads");
|
||||||
|
tty2 = new Fl_Terminal(0, 0, 300, 175);
|
||||||
|
w->resizable(tty2);
|
||||||
value2 = new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
|
value2 = new Fl_Value_Output(100, 175, 200, 25, "Max Prime:");
|
||||||
w->end();
|
w->end();
|
||||||
w->show();
|
w->show();
|
||||||
|
|
||||||
browser1->add("Prime numbers:");
|
tty1->printf("Prime numbers:\n");
|
||||||
browser2->add("Prime numbers:");
|
tty2->printf("Prime numbers:\n");
|
||||||
|
|
||||||
|
// 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...
|
||||||
|
|
||||||
// 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();
|
Fl::lock();
|
||||||
|
|
||||||
// Start threads...
|
// Start threads...
|
||||||
|
|
||||||
// One thread displaying in one browser
|
// One thread displaying in one terminal
|
||||||
fl_create_thread(prime_thread, prime_func, browser1);
|
fl_create_thread(prime_thread, prime_func, tty1);
|
||||||
|
|
||||||
// Several threads displaying in another browser
|
// Six threads displaying in another terminal
|
||||||
fl_create_thread(prime_thread, prime_func, browser2);
|
|
||||||
fl_create_thread(prime_thread, prime_func, browser2);
|
fl_create_thread(prime_thread, prime_func, tty2);
|
||||||
fl_create_thread(prime_thread, prime_func, browser2);
|
fl_create_thread(prime_thread, prime_func, tty2);
|
||||||
fl_create_thread(prime_thread, prime_func, browser2);
|
fl_create_thread(prime_thread, prime_func, tty2);
|
||||||
fl_create_thread(prime_thread, prime_func, browser2);
|
fl_create_thread(prime_thread, prime_func, tty2);
|
||||||
fl_create_thread(prime_thread, prime_func, browser2);
|
fl_create_thread(prime_thread, prime_func, tty2);
|
||||||
|
fl_create_thread(prime_thread, prime_func, tty2);
|
||||||
|
|
||||||
Fl::run();
|
Fl::run();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user