This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Programming problem with POSIX threads and stdout


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]