This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[pthread] pthread+printf may have bugs


Hi,

    I found that pthread may stop work if using printf in thread.
I'm not sure that printf is thread safe or not, so I tried to wrap the
printf with a mutex, but the problem still exists.
After I removed all printfs, everything is OK.

The testing environment:
$ uname -a
CYGWIN_NT-5.1 compname 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
Windows XP SP3 ( ver 2600 )

The testing code:


#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdarg.h>


pthread_mutex_t m;

void my_printf( const char *fmt, ... )
{
    va_list va;
    va_start( va, fmt );
    pthread_mutex_lock( &m );
    vprintf( fmt, va );
    pthread_mutex_unlock( &m );
    va_end( va );
}

void *thread_entry( void *data )
{
    my_printf( "in thread_entry %d\n", __LINE__ );
    my_printf( "in thread_entry %d\n", __LINE__ );
    my_printf( "in thread_entry %d\n", __LINE__ );
    return NULL;
}

void create_thread( pthread_t *thread_id )
{
    my_printf( "before thread create\n" );

    if ( pthread_create(
        thread_id,
        NULL,
        &thread_entry,
        NULL
        ) != 0 )
    {
        my_printf( "error" );
        exit( 0 );
    }

    my_printf( "after thread create\n" );
    my_printf( "before create_thread return\n" );
}



int main( )
{
#define N 30

    int i;
    pthread_t threads[N];

    pthread_mutex_init( &m, NULL );
    printf( "create threads\n" );
    for( i = 0; i < N; ++i )
        create_thread( &threads[i] );

    printf( "wait threads\n" );
    for( i = 0; i < N; ++i )
        pthread_join( threads[i], NULL );

    pthread_mutex_destroy( &m );
    printf( "exit...\n" );
    return 0;
}

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]