From owner-freebsd-hackers@FreeBSD.ORG Sat Aug 6 14:54:34 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D497B16A41F for ; Sat, 6 Aug 2005 14:54:34 +0000 (GMT) (envelope-from ph.schulz@gmx.de) Received: from mail.gmx.net (pop.gmx.net [213.165.64.20]) by mx1.FreeBSD.org (Postfix) with SMTP id 1D08943D46 for ; Sat, 6 Aug 2005 14:54:33 +0000 (GMT) (envelope-from ph.schulz@gmx.de) Received: (qmail invoked by alias); 06 Aug 2005 14:54:32 -0000 Received: from p54A44075.dip0.t-ipconnect.de (EHLO [192.168.1.35]) [84.164.64.117] by mail.gmx.net (mp024) with SMTP; 06 Aug 2005 16:54:32 +0200 X-Authenticated: #1954550 Message-ID: <42F4CF1D.5000501@gmx.de> Date: Sat, 06 Aug 2005 16:54:21 +0200 From: "Philip S. Schulz" User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050724) X-Accept-Language: en-us, en MIME-Version: 1.0 To: freebsd-hackers@freebsd.org Content-Type: multipart/mixed; boundary="------------010500080506060201080403" X-Y-GMX-Trusted: 0 X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: pthreads problem X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Aug 2005 14:54:35 -0000 This is a multi-part message in MIME format. --------------010500080506060201080403 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi! From the what's-wrong-with-my-code department... I'm having trouble with two threads accessing a queue. One thread writes to it while the other one reads from the queue. I'd like the consumer to run until the main thread (which is the producer) decides that the consumer thread should exit. Let me show you some example code to illustrate what I'm currently doing. The consumer runs in an endless loop until it is being cancelled by the main thread using pthread_cancel(): void *thread_routine(void *args) { while (1) { pthread_mutex_lock(&m); if (len == 0) pthread_cond_wait(¬empty, &m); printf("consumer: %i\n", --len); pthread_cond_signal(¬full); if (len == 0) pthread_cond_signal(&isempty); pthread_mutex_unlock(&m); } return (NULL); } I belive the only cancellation point here is pthread_cond_wait(), so the main thread cancels the consumer while it is waiting on the notempty condition and the mutex m will not be destroyable. pthread_mutex_lock(&m); if (len > 0) { printf("Waiting for consumer...\n"); pthread_cond_wait(&isempty, &m); } pthread_mutex_unlock(&m); pthread_cancel(t); pthread_join(t, NULL); pthread_cond_destroy(¬full); pthread_cond_destroy(¬empty); pthread_cond_destroy(&isempty); error = pthread_mutex_destroy(&m); if (error) printf("%s\n", strerror(error)); If the main thread holds the mutex over both the pthread_cancel() and pthread_join() calls, a deadlock occurs where the consumer waits on the notempty condition and the main thread waits on the consumer: pthread_cancel(t); pthread_join(t, NULL); pthread_mutex_unlock(&m); So, what is the best way to solve this problem? Introduce a variable for the while-loop like "while (running) { .. }"? Have the consumer poll if there is data in the queue? I hope somebody can help me. TIA, Phil. P.S:: Attached is the full example. --------------010500080506060201080403--