Hi,

The program demonstrate what appears to be a bug in pth:
when a thread has been canceled, but before it's been able
to run it's cleanup routines, it's priority is lowered.
This can cause the cleanup routines to never get run.

The manifestation of this is that the program below will
never finish.. the main thread gets stuck in the pth_yield()
loop... but if you change that to pth_nap() it works.

I'd appreciate any confirmation or explanation about this.

Thanks,
-Archie

__________________________________________________________________________
Archie Cobbs     *     Packet Design     *     http://www.packetdesign.com

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <pth.h>

static void     *thread_main(void *arg);
static void     thread_cleanup(void *arg);

static pth_t tid;

/*
 * Demonstration of 'thread never exits' bug in pth (?)
 */
int
main(int argc, char **argv)
{
        pth_attr_t attr;

        /* Initialize pth threads */
        if (!pth_init())
                err(1, "pth_init");

        /* Create attributes */
        if ((attr = pth_attr_new()) == NULL)
                err(1, "pth_attr_new");
        pth_attr_set(attr, PTH_ATTR_JOINABLE, 0);

        /* Create thread */
        if ((tid = pth_spawn(attr, thread_main, NULL)) == NULL)
                err(1, "pth_spawn");

        /* Destroy attributes */
        pth_attr_destroy(attr);

        /* Wait for a bit */
        pth_nap(pth_time(0, 500000));

        /* Cancel the thread */
        if (!pth_cancel(tid))
                err(1, "pth_cancel");

        /* Wait for thread to finish */
        while (tid != NULL) {
                printf("waiting for thread to exit...\n");
#if 1
                pth_yield(tid);                 /* never break out of loop */
#else
                pth_nap(pth_time(0, 500000));   /* this works normally */
#endif
        }
        printf("thread has exited.\n");

        /* Done */
        pth_kill();
        return (0);
}

static void *
thread_main(void *arg)
{
        printf("thread starting...\n");

        /* Push cleanup hook */
        if (!pth_cleanup_push(thread_cleanup, NULL)) {
                thread_cleanup(NULL);
                return (NULL);
        }

        /* Wait for cancelation */
        while (1) {
                printf("thread sleeping...\n");
                pth_nap(pth_time(1, 0));
        }
}

static void
thread_cleanup(void *arg)
{
        printf("cleaning up thread...\n");
        tid = NULL;
}


______________________________________________________________________
GNU Portable Threads (Pth)            http://www.gnu.org/software/pth/
User Support Mailing List                            [EMAIL PROTECTED]
Automated List Manager (Majordomo)           [EMAIL PROTECTED]

Reply via email to