delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/09/29/10:42:51

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
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
Message-ID: <08f101c4a632$7bb91800$640aa8c0@brunato>
From: "Mauro Brunato" <brunato AT dit DOT unitn DOT it>
To: <cygwin AT cygwin DOT 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

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/

- Raw text -


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