I have had no luck doing anything with gdb / pthreads on openbsd 3.6, and haven't found any similar complaint in the misc@/tech@ archives. The sample code that I'm using is attached below.

Is gdb (6.1 ships w/ 3.6) supposed to work with this implementation of pthreads? Calling 'info threads' while running this code doesn't work, and neither does 'thread X'.

If memory serves, ps ax should yield a different pid for each thread, but that does not seem to be the case either. Is my code simply broken, or do I misunderstand the status of pthreads in 3.6?

Any further pointers or corrections to my code would be greatly appreciated. Thanks.

ben

-------

#include <err.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

#define NUM_THREADS 5

static int count = 0;
static pthread_mutex_t mutex;

void *
incr(void *threadid)
{
        int id = (int)threadid;
        int i, n;

        printf("thread %d started: %d\n", id, getpid());

        for (i = 0; ; ++i) {
                pthread_mutex_lock(&mutex);
                n = ++count;
                pthread_mutex_unlock(&mutex);

                if (i % id == 0)
                        sleep(1);

                if (n >= 1000)
                        pthread_exit(NULL);
        }
}

int
main(int argc, char *argv[])
{
        pthread_t threads[NUM_THREADS];
        pthread_attr_t attr;
        int rc, t;

        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        pthread_mutex_init(&mutex, NULL);

        for(t = 1; t <= NUM_THREADS; ++t) {
                rc = pthread_create(&threads[t], &attr, incr, (void *)t);
                if (rc)
                        errx(1, "pthread_create");
        }

        for (t = 0; t < NUM_THREADS; ++t)
                pthread_join(threads[t], NULL);
        pthread_mutex_destroy(&mutex);
        pthread_attr_destroy(&attr);

        return (0);
}

Reply via email to