[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#233846: pthread_mutex_lock/unlock problem



Package: libc6
Version: 2.3.2.ds1-11


Hi, 
it seems that locking / unlocking the same mutex by huge number of threads 
causes dead locks.
If I create N=1529 threads (probably system-dependent) that continually 
lock the same (non-recursive) mutex, everything is ok, program runs 
forever.
But if I increase the value by one (N=1530), program freezes after few 
seconds.
This may be a pthread_self() problem (if it's used in mutex 
implementation), because I found it returns non-unique id if N>1529.
(You may try to store pthread_self() value of the main thread in a global 
variable and assert( ! pthread_equal(main_thread_id, pthread_self()) ) in 
thread procedure.)

Here is an example code demonstrating this:


#include <stdio.h>
#include <pthread.h>

#define N_THREADS       1530

pthread_mutex_t    _mxIN;

void * thread_proc_r( void * param )
{
    int i = (int) param;
    while( 1 ) {                /// lock/unlock loop
        pthread_mutex_lock( &_mxIN );
        printf( "R%XR ", i );
        if( i % 10 == 0 ) printf( "\n" );
        pthread_mutex_unlock( &_mxIN );
    }
    return NULL;
}


int main()
{
    pthread_mutexattr_t m;
    pthread_mutexattr_settype( &m, PTHREAD_MUTEX_NORMAL );
    pthread_mutex_init( &_mxIN, &m );
    pthread_mutexattr_destroy( &m );

    pthread_mutex_lock( &_mxIN );
    pthread_t th;
    for( int i=0; i<N_THREADS; i++ )    // create threads
        pthread_create( &th, NULL, thread_proc_r, (void *) i );
    pthread_mutex_unlock( &_mxIN );

    while( 1 ) {                /// lock/unlock loop
        pthread_mutex_lock( &_mxIN );
        pthread_mutex_unlock( &_mxIN );
        /// we never get here
        printf( "Main unlock\n" );
    }
    return 0;
}




////////////////////////////////
System: Debian 3.0 r2

uname -a
Linux 62.168.66.120 2.4.25 #8 Thu Feb 19 16:01:07 CET 2004 i686 GNU/Linux

I noticed this behaviour in 2.4.24 kernel and libc6 v2.3.2.ds1-10, too

gcc -v
Thread model: posix
gcc version 3.3.2 (Debian)


Thanks,
Robert Strycek




Reply to: