Mathias Hasselmann

Postings by Mathias on November 20, 2007

Re: g-thread-cancel

Richard, I'd really like to answer you, and this is the second time I'd like to do that, but unfortunatly you do not allow comments on your blog.

Well, before my karma reaches unknown depths let's add some real content to this posting:

I don't really know, why that function is missing, but my guess it, that randomly killing threads is quite dangerous, as threads share the process with your program. Stopping the thread at some random point most certainly will leave your entire program in an inconsistent state.

So common suggestion for stopping threads, is adding explicit cancelation points to your thread. Something like the following.

volatile gboolean running = TRUE;

static void* my_thread (void *data)
{
    while (running) do_work ();
}

void stop_thread (GThread *thread)
{
    running = FALSE;
    g_thread_join (thread);
}

Disclaimer: Don't take that code too verbatim. I absolutly hate threads and try to avoiding them when possible.

Btw, thanks to the genius fork() syscall, processes are dead cheap on *nix platforms. They are much more secure than threads. Specially if you think you have to interrupt them them.