From owner-freebsd-current Thu Aug 6 10:55:49 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id KAA18633 for freebsd-current-outgoing; Thu, 6 Aug 1998 10:55:49 -0700 (PDT) (envelope-from owner-freebsd-current@FreeBSD.ORG) Received: from plunger.gdeb.com (plunger.gdeb.com [153.11.11.3]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id KAA18608 for ; Thu, 6 Aug 1998 10:55:38 -0700 (PDT) (envelope-from eischen@vigrid.com) Received: from clcrtr.clc.gdeb.com ([153.11.109.11]) by plunger.gdeb.com with ESMTP (1.37.109.16/CSC-E_1.8) id AA142626082; Thu, 6 Aug 1998 13:54:42 -0400 Received: from clcrtr.clc.gdeb.com (clcrtr [153.11.109.129]) by clcrtr.clc.gdeb.com (8.8.8/8.7.3) with SMTP id NAA09576; Thu, 6 Aug 1998 13:51:57 -0400 (EDT) Message-Id: <35C9ED3D.41C67EA6@vigrid.com> Date: Thu, 06 Aug 1998 13:51:57 -0400 From: "Daniel M. Eischen" X-Mailer: Mozilla 3.0Gold (X11; I; FreeBSD 2.2.6-STABLE i386) Mime-Version: 1.0 To: shmit@kublai.com Cc: freebsd-current@FreeBSD.ORG Subject: Re: Pthreads woes revisited. Content-Type: multipart/mixed; boundary="------------446B9B3D2781E494167EB0E7" Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG This is a multi-part message in MIME format. --------------446B9B3D2781E494167EB0E7 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit > 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. > But your not yielding the CPU after the pthread_cond_signal(). The waiter thread (thr_wait) should also take the mutex lock (busy) before the pthread_cond_wait(). See the comments in the revised copy of your code. Dan Eischen eischen@vigrid.com --------------446B9B3D2781E494167EB0E7 Content-Type: text/plain; charset=us-ascii; name="thr_test.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="thr_test.c" #include #include #include #include pthread_cond_t wakeup; pthread_mutex_t busy; void * thr_routine(void *arg) { int rc; /* * The mutex should be taken before the condition wait. * The condition wait will atomically release the mutex and * retake it when awoken. */ pthread_mutex_lock (&busy); for (;;) { printf("%d: Waiting for signal.\n", (int) 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", (int) 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; } /* Allow the other thread to run. */ pthread_yield (); } return 0; } --------------446B9B3D2781E494167EB0E7-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message