Re: thread problem



Bart Vandewoestyne wrote:

The problem is simple:

I have a START and a STOP button in my gtk application.  The start
button has a callback in which a g_thread is being started.  Within that
thread, I'm continuously downloading a stream until the user decides to
stop by pressing the 'STOP' button.

Now the problem is that i don't know how to interrupt the continously
downloading thread by pressing the stop button.

Obviously, the callback of STOP has to do something, but i don't know
what.  Is there some way that i can make the stop button stop the other
thread that is running (somehow the same as you would end the
application by pressing Ctrl-C) ?

Full source code with Makefile is under the src/ directory in
mc303.ulyssis.org/downloads/mms_gui.tar.gz for those who want to
investigate.  grep for the 'go_clicked' callback to understand what I'm
talking about.

Greetzzz,
Bart

you could take a number of aproaches, here are 2

1.) if you dont care about the child thread's data at the time
you press "STOP" then you could use kill(2).

2.) if you want to do things a little cleaner you could:

   in child thread {
       //loop in small itterations
       //and always check:
       g_mutex_lock(stop_mutex);
       if (stop_variable) {
// cleanup code: g_mutex_unlock(stop_mutex);
           exit(0);
       }
       g_mutex_unlock(stop_mutex);
   }

   in parent thread {
       g_mutex_lock(stop_mutex);
       stop_variable = TRUE;
       g_mutex_unlock(stop_mutex);
       g_thread_join(...);
   }


Cheers,
                                   -Tristan






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]