public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* [pthread] pthread+printf may have bugs
@ 2008-12-24  8:55 陈晓晖
  2008-12-25  2:53 ` 陈晓晖
  0 siblings, 1 reply; 3+ messages in thread
From: 陈晓晖 @ 2008-12-24  8:55 UTC (permalink / raw)
  To: cygwin

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-12-25  4:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-24  8:55 [pthread] pthread+printf may have bugs 陈晓晖
2008-12-25  2:53 ` 陈晓晖
2008-12-25  4:16   ` Christopher Faylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).