Hello,
I have a question that is not really about fltk, but hope to get some helps or
approaches from here.
My program has two child threads, thread1 and thread2 spawned from the main
thread. when a button from the main thread is clicked, it checks user's
selections on three check boxes. The checks can be in any combination, but they
always be executed in sequence.
Thread1 is in charge of running three heavy calculations and thread2 is in
charge of displaying results immediately after each calculation to the GUI. I
have both threads created in main in the suspended states. When the button is
clicked, thread1 starts doing his jobs. Once thread1 finishes his first job, it
resumes thread2 to update the results to the screen. In the mean while, thread2
continues to do his second job.
The question I have is how do I put thread2 to pause until thread1 finishes his
second job, then thread2 can update the results to screen, then continue one in
this fashion. I do not want to use busy wait loop, because it will eat up lots
of cpu time. What is the best approach here in this case?
Moreover, once thread1 finishes all his tasks, user can input different
parameters and restart the calculation again with different selections.
My test code is as followed:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Hor_Slider.H>
#include <FL/math.h>
#include <FL/Fl_Int_Input.h>
#include <FL/Fl_Output.h>
#include <FL/Fl_Button.h>
#include <FL/Fl_Check_Button.h>
#include <FL/fl_ask.H>
#include <stdio.h>
#include <cstdlib>
#include <process.h>
#include <windows.h>
Fl_Output* output;
Fl_Window *window_2;
Fl_Window *window_3;
Fl_Int_Input *idIniOrder;
Fl_Output *cb_infoN;
Fl_Button *start;
Fl_Check_Button *check1;
Fl_Check_Button *check2;
int processID = 0;
int processAC = 0;
const int numThreads = 2;
HANDLE threadArray[numThreads];
static void start_cb(Fl_Button *b, void *p);
static void check1_cb(Fl_Button *b, void *p)
{
processID = check1->value();
}
static void check2_cb(Fl_Button *b, void *p)
{
processAC = check2->value();
}
void make_first_window()
{
char tmp_s[10] = "19";
start = new Fl_Button(1,1,65,20, "&Start");
start->callback( (Fl_Callback*) start_cb );
start->labelsize(14);
check1 = new Fl_Check_Button(1, 40, 65, 20," System ID");
check1->when(FL_WHEN_CHANGED);
check1->labelsize(13);
check1->callback((Fl_Callback*) check1_cb);
check2 = new Fl_Check_Button(1, 70, 65, 20," PFF AC");
check2->when(FL_WHEN_CHANGED);
check2->labelsize(13);
check2->callback((Fl_Callback*) check2_cb);
}
void window_1_cb(Fl_Widget *o, void *p) {
exit(0);
}
void window_2_cb(Fl_Widget *o, void *p) {
exit(0);
}
static void start_cb(Fl_Button *b, void *p)
{
if (processID)
{
window_2->show();
ResumeThread(threadArray[0]);
}
if (processAC)
{
window_3->show();
}
}
unsigned WINAPI RunProcesses(void*)
{
char msg[256];
for (int i=0; i<100000; i++)
{
sprintf(msg, "%d", i);
Fl::lock(); // Obtain a lock before
we access the browser widget...
output->value(msg);
output->redraw();
Fl::unlock(); // Release the lock...
Fl::awake();
}
//SuspendThread(threadArray[0]);
return (unsigned)i;
}
unsigned __stdcall PlotGraphs(void* myID)
{
return (unsigned)myID;
}
int main(int argc, char **argv) {
Fl::lock();
unsigned threadOneID;
unsigned threadTwoID;
DWORD rc;
threadArray[0] = (HANDLE)_beginthreadex(NULL, 0, &RunProcesses, NULL,
CREATE_SUSPENDED, &threadOneID);
if ( threadArray[0] == 0 )
printf("Failed to create thread 1\n");
//threadArray[1] = (HANDLE)_beginthreadex(NULL, 0, PlotGraphs, (LPVOID)2U,
CREATE_SUSPENDED, &threadTwoID);
Fl_Window *window = new Fl_Window(100, 100, 200, 100, "1 Window");
window->begin();
make_first_window();
window->callback(window_1_cb);
window->end();
window->show(argc,argv);
window_2 = new Fl_Window(230, 100, 200, 100, "2 Window");
window_2->begin();
output = new Fl_Output(10,10, 100, 25);
output->color(FL_BACKGROUND_COLOR);
output->labelsize(14); // 14 is default
output->type(14);
output->textsize(14);
window_2->end();
window_2->hide();
window_2->resizable(window_2);
window_3 = new Fl_Window(250, 120, 200, 100, "3 Window");
window_3->begin();
window_3->end();
window_3->hide();
window_3->resizable(window_2);
return Fl::run();
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk