Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 19 Oct 1998 14:35:40 -0400 (EDT)
From:      HighWind Software Information <info@highwind.com>
To:        current@FreeBSD.ORG
Subject:   Another Serious libc_r problem
Message-ID:  <199810191835.OAA20137@highwind.com>

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

We found another libc_r problem which is spinning the CPU at 100%.
This makes it impossible for our application to work at all.

FreeBSD 3.0 kernel from August. The LATEST libc_r. Tested this on
another FreeBSD with a much more recent kernel. Same result. The CPU
spins and the threads don't get the condition.

Program works fine on other operating systems. After the 3 second
start up, it prints nice stream of "Signalling" and "Got Condition!".

On FreeBSD 3.0, it prints "Signalling" twice and then spins the CPU.

Help is always appreciated. 

-Rob

---


/* 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;
}

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



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