Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Aug 1998 13:01:22 -0400
From:      Brian Cully <shmit@kublai.com>
To:        current@FreeBSD.ORG
Subject:   Pthreads woes revisited.
Message-ID:  <19980806130122.38511@kublai.com>

next in thread | raw e-mail | index | archive | help

--FyWQ0h3ruR435lwh
Content-Type: text/plain; charset=us-ascii

I've put together a small C program that demonstrates the problems
I described in my last message. You can find the program attached
to this message.

Note the location of the `sleep(1)' call, it's important. If it moves
outside of the mutex lock/unlock bits, everything functions normally,
but if it gets called after the lock, but before the unlock, the first
signal is missed.

Looks like broken signal handling of some variety.

-- 
Brian Cully						<shmit@erols.com>
``And when one of our comrades was taken prisoner, blindfolded, hung
  upside-down, shot, and burned, we thought to ourselves, `These are the
  best experiences of our lives''' -Pathology (Joe Frank, Somewhere Out There)

--FyWQ0h3ruR435lwh
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="ptest2.c"

#include <pthread.h>
#include <stdio.h>
#include <string.h>

pthread_cond_t wakeup;
pthread_mutex_t busy;

void *
thr_routine(void *arg)
{
	int rc;

	for (;;) {
		printf("%d: Waiting for signal.\n", pthread_self);
		rc = pthread_cond_wait(&wakeup, &busy);
		if (rc) {
			fprintf(stderr,
				"ERROR: couldn't wait for condition: %s.\n",
				strerror(rc));
			return NULL;
		}
		printf("%d: Got signal.\n", pthread_self);
	}

	return NULL;
}

int
main(int argc, char *argv[])
{
	int rc;
	pthread_t thread_id;

	rc = pthread_cond_init(&wakeup, NULL);
	if (rc) {
		fprintf(stderr,
			"ERROR: couldn't create condition variable: %s.\n",
			strerror(rc));
		return 1;
	}

	rc = pthread_mutex_init(&busy, NULL);
	if (rc) {
		fprintf(stderr, "ERROR: couldn't create mutex: %s.\n",
			strerror(rc));
		return 1;
	}

	rc = pthread_create(&thread_id, NULL, thr_routine, NULL);
	if (rc) {
		fprintf(stderr, "ERROR: couldn't create thread: %s.\n",
			strerror(rc));
		return 1;
	}

	for (;;) {
		printf("MASTER: Acquiring lock.\n");
		rc = pthread_mutex_lock(&busy);
		if (rc) {
			fprintf(stderr, "ERROR: couldn't lock mutex: %s.\n",
				strerror(rc));
			return 1;
		}

		sleep(1);

		printf("MASTER: Releasing lock.\n");
		rc = pthread_mutex_unlock(&busy);
		if (rc) {
			fprintf(stderr, "ERROR: couldn't unlock mutex: %s.\n",
				strerror(rc));
			return 1;
		}
		printf("MASTER: Sending signal.\n");
		rc = pthread_cond_signal(&wakeup);
		if (rc) {
			fprintf(stderr, "ERROR: couldn't signal thread: %s.\n",
				strerror(rc));
			return 1;
		}
	}

	return 0;
}

--FyWQ0h3ruR435lwh--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19980806130122.38511>