From owner-freebsd-threads@FreeBSD.ORG Tue Apr 27 10:18:09 2010 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A35961065674 for ; Tue, 27 Apr 2010 10:18:09 +0000 (UTC) (envelope-from martin@lispworks.com) Received: from lwfs1-cam.cam.lispworks.com (mail.lispworks.com [193.34.186.230]) by mx1.freebsd.org (Postfix) with ESMTP id 419028FC0A for ; Tue, 27 Apr 2010 10:18:08 +0000 (UTC) Received: from higson.cam.lispworks.com (IDENT:U2FsdGVkX18d9lOEAi+VV8iePQUTCyoB5p/xw+YkaWk@higson [192.168.1.7]) by lwfs1-cam.cam.lispworks.com (8.12.9p2/8.12.9) with ESMTP id o3RA7oQc047811; Tue, 27 Apr 2010 11:07:50 +0100 (BST) (envelope-from martin@lispworks.com) Received: from higson.cam.lispworks.com by higson.cam.lispworks.com (8.13.1) id o3RA7o0F021470; Tue, 27 Apr 2010 11:07:50 +0100 Received: (from martin@localhost) by higson.cam.lispworks.com (8.13.1/8.13.1/Submit) id o3RA7nLK021467; Tue, 27 Apr 2010 11:07:49 +0100 Date: Tue, 27 Apr 2010 11:07:49 +0100 Message-Id: <201004271007.o3RA7nLK021467@higson.cam.lispworks.com> From: Martin Simmons To: freebsd-threads@freebsd.org In-reply-to: <6AD0A971B01FA1DE632BAF65@HPQuadro64.dmpriest.net.uk> (message from Karl Pielorz on Tue, 27 Apr 2010 09:00:35 +0100) References: <6AD0A971B01FA1DE632BAF65@HPQuadro64.dmpriest.net.uk> Subject: Re: Advice / best practice - thread connection pools / mutexes X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Apr 2010 10:18:09 -0000 Assuming it is safe share MySQL connections between threads like that (I don't know), then using a data structure to track the unused connections sounds like the simplest approach. Protect access to the data structure with a mutex and use a condition variable to wait (with pthread_cond_wait) for a free connection if they are all in use. When a connection is freed, use pthread_cond_signal to wake a waiting thread. Note that both pthread_cond_wait and pthread_cond_signal must be called while holding the mutex to prevent missed wakups. __Martin