> 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.

        If you're going to do anything with the screen,
        I'd suggest doing it in the main FLTK thread.

        You can have the main thread start an FLTK timer
        that triggers every 1/30th of a second (or whatever)
        to watch a buffer that thread #1 appends data to
        whenever its ready.

        Use a lock to make sure the main loop doesn't
        try to read data while thread #1 is writing it.

> 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?

        If you really want two extra threads in addition
        to the main thread, seems like a mutex lock would
        be good for this.

        Basically thread #2 blocks on a lock that thread #1
        creates. When thread #1 has data ready, it unlocks,
        waking thread #2 so it can do its display work.

        You may need two locks to ensure one thread never
        gets ahead of the other.

        This should allow both thread #1 and thread #2 to
        do their work at the same time, while preventing
        one from getting ahead of the other.

        With the pthreads library I think this would be
        done with pthread_mutex_lock()/unlock.

        Watch for races and deadlocks when you do this
        sort of thing.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to