Question

I'm currently working on a sound signal visualizer. After getting the signal, I need to plot both its waveform and spectrum on two different windows.Thus I implemented a Plotter class, which creates its own thread for each instance when constructed. But now I'm running into troubles because many of FLTK's features seemed to be thread-unsafe, because it crashes on some class method calls. (Such as set axis scale)

The model I'm using now:

Plotter 1 -> ctor() -> create thread
                                     \
Plotter 2 -> ctor() -> create thread----> PlotterThread(Plotter*this) - 
                                     /                                |
Plotter 3 -> ctor() -> create thread                                  |
                                                                      |
                                                     create fltk window and init object 

Is this model even practicable? I have no idea now... Thanks.

Was it helpful?

Solution

You obviously did not read the most important part of FLTK documentation with regards to concurrent programming: http://www.fltk.org/doc-1.3/advanced.html#advanced_multithreading . Please read it carefully.

You have two alternatives:

  1. Use Fl::awake() to send message to the main (gui) thread. Main thread can check the message using Fl::thread_message() and create whatever widget you like.

  2. Between calls to Fl::lock() and Fl::unlock() call FLTK methods.

Finally, you have a very nice demo in the $FLTK_SRC/test directory, where $FLTK_SRC is the directory where you hold FLTK source tree. If you do not have it, download it.

OTHER TIPS

I worked out a solution to this problem:

Create only one thread (the plotter thread) for all the Plotters. For Plotters after the first one, they use QueueUserAPC to post a construction call to the plotter thread, because seems FLTK only allows creating UI elements in the main thread. (the plotter thread)

Meanwhile, the plotter thread use

while( Fl::wait() > 0)
    SleepEx( 0, TRUE);

instead of

Fl::run();

to poll the APC requests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top