From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1231 invoked by alias); 24 Dec 2008 08:55:17 -0000 Received: (qmail 1223 invoked by uid 22791); 24 Dec 2008 08:55:17 -0000 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 Received: from mail-bw0-f32.google.com (HELO mail-bw0-f32.google.com) (209.85.218.32) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 24 Dec 2008 08:54:42 +0000 Received: by bwz13 with SMTP id 13so2759284bwz.2 for ; Wed, 24 Dec 2008 00:54:38 -0800 (PST) Received: by 10.181.216.14 with SMTP id t14mr2103403bkq.103.1230108878819; Wed, 24 Dec 2008 00:54:38 -0800 (PST) Received: by 10.181.213.11 with HTTP; Wed, 24 Dec 2008 00:54:38 -0800 (PST) Message-ID: <6aa8c88e0812240054m6a13857uee028380f5d04862@mail.gmail.com> Date: Wed, 24 Dec 2008 08:55:00 -0000 From: "=?GB2312?B?s8LP/urN?=" To: cygwin@cygwin.com Subject: [pthread] pthread+printf may have bugs MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-IsSubscribed: yes Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com X-SW-Source: 2008-12/txt/msg00593.txt.bz2 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 #include #include #include #include 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/