Attached below - bigger than I'd intended, I got carried away.

Compile as:

  fltk-config --compile thread-test.cxx




/* Simple Thread Test Code - v3 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

/* Set up some "thread portability" - this is derived from fltk's own
threads.h test stub */
#ifdef WIN32
#  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);
}
#  define PAUSE(x)      Sleep(x)  // delay in milliseconds
#  define SCHED_YIELD   Sleep(0)

#else /* OSX, linux, and other Unix's - assume pthreads */
#  include <pthread.h>
#  include <sched.h>  // maybe necessary for sched_yield

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);
}
#  define PAUSE(x)      usleep((x)*1000) // delay in milliseconds
#  define SCHED_YIELD   sched_yield()
#endif

/* Now create the fltk GUI elements... */
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Value_Output.H>

#define MAX_THRDS 4

/* Thread control vars - crude IPC, can be done better! */
static volatile int keep_running = -1;
static volatile int active_thread[MAX_THRDS];

/* Some global GUI elements... */
static Fl_Double_Window *main_win=(Fl_Double_Window *)0;
static Fl_Value_Output *thrd_out[MAX_THRDS];

/* Generic thread task - all the threads in the example use this
function.
 * In real life, each thread might call a different task... */
void* thread_task(void* p) {
  long id = (long)p;
  int count = 0;
  printf("Start thread %ld\n", id+1);
  fflush(stdout);

  while((keep_running) && (active_thread[id])) {
    count++;
    Fl::lock();
    thrd_out[id]->value(count);
    Fl::unlock();
    Fl::awake();
    PAUSE(1000); /* delay for a second - in real life, you'd do some
work... */
    printf("Hello from thread %ld\n", (id+1));
    fflush(stdout);
  }
  printf("Thread %d stop\n", (id+1));
  fflush(stdout);
  return 0; /* Thread expires */
} // thread_task

/* Exit callback to stop all the threads and close the main window */
static void cb_exit_bt(Fl_Button*, void*) {
  keep_running = 0; /* make any pending threads expire */
  main_win->hide();
  printf("Closing\n");
} // cb_exit_bt

static void cb_add_bt(Fl_Button *b, void *vi) {
  // new thread button callback
  int id = (int)vi;
  Fl_Button *bt = (Fl_Button *)b;
  int state = bt->value();

  if(state) // start a thread
  {
    Fl_Thread thread_id;
    active_thread[id] = -1;
    fl_create_thread(thread_id, thread_task, (void *)id);
  }
  else // stop the thread
  {
    active_thread[id] = 0;
  }
} // cb_add_bt

int main(int argc, char **argv) {

  Fl::lock(); /* acquire the master lock - wakes up fltk thread support
*/
  main_win = new Fl_Double_Window(316, 231, "Threads Tester");
  main_win->begin();

  Fl_Button *exit_bt = new Fl_Button(230, 190, 64, 20, "Close");
  exit_bt->callback((Fl_Callback*)cb_exit_bt);

  for (int idx = 0; idx < MAX_THRDS; idx++)
  {
    int xx = 20 + idx * 75;
    Fl_Button *add_bt = new Fl_Button(xx, 15, 64, 20, "Go");
    add_bt->type(FL_TOGGLE_BUTTON);
    add_bt->callback((Fl_Callback*)cb_add_bt, (void*)idx);

    thrd_out[idx] = new Fl_Value_Output(xx, 45, 64, 30);
  }

  main_win->end();
  main_win->callback((Fl_Callback*)cb_exit_bt);
  main_win->show(argc, argv);

  keep_running = -1; // set this zero to kill all the child threads

  // clear out the thread list
  memset((void*)active_thread, 0, sizeof(active_thread));

  // run the fltk event loop
  return Fl::run();
} // main

/* End of File */



SELEX Galileo Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 
3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to