Re: thread cancellation



On Mon, 2003-09-15 at 15:21, Colin Walters wrote:
> 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).

It seems like it would be useful to cancel foreach operations in
GHashTable.  Maybe the API could be enhanced for glib 2.4?  It might be
API creep though.

As for libxml, you can feed data into it incrementally by using a push
parser context instead of a doc parser context:

        xmlParserCtxt *ctxt;
        
        ctxt = xmlCreatePushParserCtxt (&sax_vtable, user_data, NULL, 0, NULL);
        
        while (data && !cancelled) {
                xmlParseChunk (ctxt, data, size, 0);
        }
        
        xmlParseChunk (ctxt, NULL, 0, 1);
        xmlFreeParserCtxt (ctxt);

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

I don't think there's really any clean way to do this in C.  

If all you're worried about is managing memory in local stack frames,
you could allocate everything with alloca() and use longjmp().

If that's too scary or is otherwise untenable, you could create a macro
which checks to see if you've been cancelled in certain parts and then
does a goto to a label toward the end of the function where you can
clean up after yourself.

Joe




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