Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 19 Oct 1998 11:30:17 -0700 (PDT)
From:      info@highwind.com
To:        freebsd-gnats-submit@FreeBSD.ORG
Subject:   kern/8375: pthread_cond_wait() spins the CPU
Message-ID:  <199810191830.LAA03985@hub.freebsd.org>

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

>Number:         8375
>Category:       kern
>Synopsis:       pthread_cond_wait() spins the CPU
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          open
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 19 11:40:00 PDT 1998
>Last-Modified:
>Originator:     Robert Fleischman
>Organization:
HighWind Software, Inc.
>Release:        3.0 with LATEST libc_r
>Environment:
% uname -a
FreeBSD zonda.highwind.com 3.0-19980831-SNAP FreeBSD 3.0-19980831-SNAP #0: Mon Aug 31 14:03:19 GMT 1998     root@make.ican.net:/usr/src/sys/compile/GENERIC  i386

>Description:
If a number of threads are waiting for a condition, the application spins
the CPU at 100% and eventually hangs the application. I've included
a test program below which illustrates the problem.
>How-To-Repeat:
/* Illustration of FreeBSD pthread_cond_wait() bug
   
   This program sets up a conditional wait and fires off a dozen threads
   that simply wait for the condition. Once the threads are started, 
   the main thread loops signalling the condition once a second.

   Normally, this should result in "Signalling" and "Got Condition"
   being printed once a second. However, because of some bugs in
   FreeBSD, the pthread_cond_wait() spins the CPU and no progress is
   made.

   g++ -o condWaitBug -D_REENTRANT -D_THREAD_SAFE -g -Wall condWaitBug.C -pthread

*/

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

pthread_mutex_t lock;
pthread_cond_t condition;

static void *condThread(void *)
{
    // Wait until we are signalled, then print.
    while (true) {
	assert(!::pthread_cond_wait(&condition, &lock));
	::printf("Got Condition!\n");
    }
}

int main(int, char **)
{
    // Initialize Lock
    pthread_mutexattr_t lock_attr;
    assert(!::pthread_mutexattr_init(&lock_attr));
    assert(!::pthread_mutex_init(&lock, &lock_attr));
    assert(!::pthread_mutexattr_destroy(&lock_attr));

    // Initialize Condition
    pthread_condattr_t cond_attr;
    assert(!::pthread_condattr_init(&cond_attr));
    assert(!::pthread_cond_init(&condition, &cond_attr));
    assert(!::pthread_condattr_destroy(&cond_attr));

    // Lock the lock
    assert(!::pthread_mutex_lock(&lock));

    // Spawn off a dozen threads to get signalled
    for (int j = 0; j < 12; ++j) {
	pthread_t tid;
	pthread_attr_t attr;
	assert(!::pthread_attr_init(&attr));
	assert(!::pthread_create(&tid, &attr, condThread, 0));
	assert(!::pthread_attr_destroy(&attr));
    }

    // Sleep for 3 seconds to make sure the threads started up.
    timeval timeout;
    timeout.tv_sec = 3;
    timeout.tv_usec = 0;
    ::select(0, 0, 0, 0, &timeout);

    for (int k = 0; k < 60; ++k) {
	::printf("Signalling\n");
	::pthread_cond_signal(&condition);

	// Sleep for a second
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;
	::select(0, 0, 0, 0, &timeout);
    }

    return EXIT_SUCCESS;
}

>Fix:

>Audit-Trail:
>Unformatted:

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



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