Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Message-ID: <08f101c4a632$7bb91800$640aa8c0@brunato>
From: "Mauro Brunato" <brunato@dit.unitn.it>
To: <cygwin@cygwin.com>
Subject: Programming problem with POSIX threads and stdout
Date: Wed, 29 Sep 2004 16:42:02 +0200
Organization: =?Windows-1252?Q?Universit=E0_di_Trento?=
MIME-Version: 1.0
Content-Type: text/plain; format=flowed; charset="Windows-1252"; reply-type=original
Content-Transfer-Encoding: 7bit

The following has probably already been noticed by someone, but I could not 
find any reference (at least, comprehensible to me), so I apologize if my 
problem has already been taken care of.

Consider the following code:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>

    void *mythread (void *p)
    {
      puts ("Begin thread.");
      sleep (2);
      puts ("End thread.");
      return NULL;
    }

    int main (void)
    {
      pthread_t threadID;
      puts ("Begin program.");
      pthread_create (&threadID, NULL, mythread, NULL);
      sleep(1);
      puts ("Mid program.");
      sleep (2);
      puts ("End program.");
      return 0;
    }

In my intention, it should write the following (the sleeps take care of the 
timing):

    Begin program.
    Begin thread.
    Mid program.
    End thread.
    End program.

However, the last line (which is executed after the thread has exited) is 
not printed. I also tried with files and with redirection. If I repeat the 
"sleep(2)" twice in the main program, the program actually takes longer, so 
I am sure that execution of main still proceeds after the thread has 
stopped; however, the standard output seems to have been closed by the 
thread termination.

I have an up-to-date Cygwin system (1.5.11-1 DLL, GCC 3.3.3 with POSIX 
thread model); the compilation command that I used is

    gcc -Wall -o ttest ttest.c

I also tried putting debug or optimization flags (-g, -O, -O2) and put an 
explicit "-lpthread" at the end, with no luck.

I'm a bit new to pthreads, so probably it's my fault, or maybe this behavior 
is in the specifications; however, under Linux I get the whole output as I 
expect. Is there a way to get things right?

Thank you,

  Mauro.


--
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/

