From owner-freebsd-threads@FreeBSD.ORG Sun Feb 1 08:50:22 2004 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7801916A4CE for ; Sun, 1 Feb 2004 08:50:22 -0800 (PST) Received: from mail.pcnet.com (mail.pcnet.com [204.213.232.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id D6EA743D4C for ; Sun, 1 Feb 2004 08:50:20 -0800 (PST) (envelope-from eischen@vigrid.com) Received: from mail.pcnet.com (mail.pcnet.com [204.213.232.4]) by mail.pcnet.com (8.12.10/8.12.1) with ESMTP id i11GoCiw004861; Sun, 1 Feb 2004 11:50:12 -0500 (EST) Date: Sun, 1 Feb 2004 11:50:12 -0500 (EST) From: Daniel Eischen X-Sender: eischen@pcnet5.pcnet.com To: Craig Rodrigues In-Reply-To: <20040201152105.GA2370@crodrigues.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: Julian Elischer cc: freebsd-threads@freebsd.org Subject: Re: pthread_create() blocks if maxthreads reached? X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 01 Feb 2004 16:50:22 -0000 On Sun, 1 Feb 2004, Craig Rodrigues wrote: > On Sat, Jan 31, 2004 at 11:27:39PM -0800, Julian Elischer wrote: > > > > > > If I link with -lpthread, the program seems to > > > block in the pthread_create() call, instead of > > > returning EAGAIN (to indicate that a new thread cannot > > > be created due to hitting some system limit). > > > > is that what you expected? > > > No that is not what I expected. I expected > that pthread_create() should either: > - if it creates the thread successfully, return 0 > - if it cannot create the thread, return EAGAIN > if a resource limit is it, or EINVAL if bad parameters > were passed down into the pthread_create() call > > > I never expected that pthread_create() would block. You're missing the point. pthread_create() doesn't block; you're application blocks because it has consumed the limit of kernel threads allowed. It just so happens that the only runnable thread is making a lot of calls to pthread_create(), but it could be doing anything and it would still block. #include #include #include #include #include #include static pthread_mutex_t sync_lock; static void * worker(void *arg) { pthread_mutex_lock(&sync_lock); pthread_mutex_unlock(&sync_lock); select(0, NULL, NULL, NULL, NULL); return (NULL); } int main(int argc, char** argv) { pthread_t *threads; int i, n; if (argc > 1) { n = strtoimax(argv[1], NULL, 10); printf("Using %d threads (parameter)\n", n); } else { n = 5; printf("Using %d threads (default)\n", n); } threads = (pthread_t *)malloc(sizeof(pthread_t) * n); pthread_mutex_init(&sync_lock, NULL); pthread_mutex_lock(&sync_lock); /* Create and start n threads. */ for (i = 0; i < n; i++) { if (pthread_create(&threads[i], NULL, worker, NULL) != 0) { err(1, NULL); } } /* Let the threads loose. */ printf("Created %d threads; letting them loose.\n", i); pthread_mutex_unlock(&sync_lock); sleep(10); printf("Main thread waking up after sleeping\n"); /* Join the threads. */ for (i = 0; i < n; i++) { if (pthread_join(threads[i], NULL) != 0) { err(1, NULL); } printf("Joined thread %d\n", i); } free(threads); return (0); }