delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/12/24/03:55:22

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=2.2 required=5.0 tests=AWL,BAYES_00,CHARSET_FARAWAY_HEADER,SARE_MSGID_LONG40,SPF_PASS
X-Spam-Check-By: sourceware.org
Message-ID: <6aa8c88e0812240054m6a13857uee028380f5d04862@mail.gmail.com>
Date: Wed, 24 Dec 2008 16:54:38 +0800
From: "=?GB2312?B?s8LP/urN?=" <xhchen8018 AT gmail DOT com>
To: cygwin AT cygwin DOT com
Subject: [pthread] pthread+printf may have bugs
MIME-Version: 1.0
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019