thread cancellation


My application uses a number of threads.  For instance, one iterates
over an XML file and performs actions.  Another iterates over an
in-memory database based on GHashTables and generates a result set from
a query.  Yet another recursively traverses a directory and performs an
action for each file.

The problem:  I need a way to cancel these operations, for example when
the user wants to quit the application, or when they want to cancel a
running database query and start another.  What I am doing now is
passing a pointer to a boolean variable along with these threads.  The
variable is then checked at various points throughout the operation, and
if it's set we try to back out and free any allocated resources.

However, while this approach works acceptably well for the
directory-traversal thread, the first two cases are more problematic. 
There's no way as far as I can see to tell libxml "stop the current
traversal".  Nor can I stop iterating over a GHashTable right in the

This problem can be solved by adding the ability to perform these
operations incrementally; for instance, if GHashTable had an iterator
interface, or if I could feed data to the libxml SAX parser
incrementally instead of all at once (I might switch to GMarkup, which
allows this).

Still though, even this feels kind of unclean.  It feels like what I
really want here are exceptions.  But since my application is written in
C, that just leaves me with setjmp/longjmp, which will be quite
difficult to use because I will have to be careful to free any allocated
memory used along the way.

Has anyone else solved this problem in a different way?  Opinions?

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