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

* Re: [pthread] pthread+printf may have bugs
  2008-12-24  8:55 [pthread] pthread+printf may have bugs 陈晓晖
@ 2008-12-25  2:53 ` 陈晓晖
  2008-12-25  4:16   ` Christopher Faylor
  0 siblings, 1 reply; 3+ messages in thread
From: 陈晓晖 @ 2008-12-25  2:53 UTC (permalink / raw)
  To: cygwin

It is strange that the error disappeared if replacing any cygwin1.dll
on the snapshot page
    http://cygwin.com/snapshots/
For these versions, I see a more line "Huh?  No /etc/fstab file?
Using default root and cygdrive prefix..." was printed.

By the way, does anybody know where can I find the pthread source for
cygwin? is it pthread-win32? Thanks ..
I want to look into it.


2008/12/24 陈晓晖 <xhchen8018@gmail.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.
>

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

* Re: [pthread] pthread+printf may have bugs
  2008-12-25  2:53 ` 陈晓晖
@ 2008-12-25  4:16   ` Christopher Faylor
  0 siblings, 0 replies; 3+ messages in thread
From: Christopher Faylor @ 2008-12-25  4:16 UTC (permalink / raw)
  To: cygwin

On Thu, Dec 25, 2008 at 10:52:14AM +0800, "?????????" wrote:
>It is strange that the error disappeared if replacing any cygwin1.dll
>on the snapshot page
>    http://cygwin.com/snapshots/
>For these versions, I see a more line "Huh?  No /etc/fstab file?  Using
>default root and cygdrive prefix..." was printed.

The snapshots are based on version 1.7 of the Cygwin DLL:

http://cygwin.com/ml/cygwin-announce/2008-12/msg00028.html

This version has a few pthreads fixes.

>By the way, does anybody know where can I find the pthread source for
>cygwin? is it pthread-win32? Thanks ..
>I want to look into it.

The source code for the snapshots is available at the same place where
you downloaded the snapshot.  The source code for Cygwin 1.5.25 is
available for download via setup.exe.

--
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).