1

3 threads:

pthread_create(&thread1, 
               &NULL,        
               Thread1,
               NULL);
pthread_create(&thread2, 
               &NULL,        
               Thread2,
               NULL);
pthread_create(&thread3, 
               &NULL,        
               Thread3,
               NULL);

printf("\n\nThreads Created\n");

    pthread_join(thread1,0);
    printf("Joined Thread1\n");

    pthread_join(thread2,0);
    printf("Joined Thread2\n");

    pthread_join(thread3,0);
    printf("Joined Thread3\n");

The 3 threads run for a while and based on the output to the console, appear to be working.

Eventually, thread 1 and 2 die after their work is done (appears correct as output from maint() "Joined Thread 1/2" is displayed)

Now, thread 3 still has some work to do and looks good. Then, close to processing it's last few items, it "appears" that thread 3 just hangs. It will be printing something out to the console and won't even finish the sentence.

Thread 3 has is a small sleep, locking and unlocking of a mutex (that thread 1 and 2 were using) and a conditional wait. Does not appear to be on the conditional wait as I print something out directly before it is called and do not see that.

It seems like it is the sleep, giving up the CPU but then never coming back.....?

Any other possibilities or reasons why?

Any solutions?

Thanks.

P.S.
  • 384
  • 3
  • 18
  • 1
    Actually, it _could_ be that wait. It may be that standard output isn't being flushed before you wait. Can you `fflush()` whichever stream you print to before waiting on that condition? – Iwillnotexist Idonotexist Jun 15 '14 at 00:08
  • 1
    Also could you please post up the code for threads 1, 2 and 3 if that's not too complicated, or at least the relevant parts? – Iwillnotexist Idonotexist Jun 15 '14 at 00:14
  • @iwillnotexist-idonotexist: ok let me check the wait and try a flush before it, I'm not suppose to post the code.... – P.S. Jun 15 '14 at 00:20
  • 1
    Just the lock/unlock/wait logic will be enough. – Iwillnotexist Idonotexist Jun 15 '14 at 00:21
  • @iwillnotexist-idonotexist: you are right it is the wait!, didn't know I had to use fflush like this – P.S. Jun 15 '14 at 00:27
  • @iwillnotexist-idonotexist: ...so I have to fflush after every printf() ? – P.S. Jun 15 '14 at 00:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55636/discussion-between-p-s-and-iwillnotexist-idonotexist). – P.S. Jun 15 '14 at 00:32
  • I'm in the middle of something important atm but will get back ASAP. – Iwillnotexist Idonotexist Jun 15 '14 at 01:22
  • IIRC, printing a newline with also flush the output buffer. For that reason, it's always a good idea to end your debug print statements with a \n. – Tyler Jun 15 '14 at 18:13
  • @tyler: The buffer would not flush with \n for me....not sure why bu I had to call fflsuh() for it to work. Not sure what the difference was?.. – P.S. Jun 15 '14 at 18:39
  • 1
    Oops. Should have done my research before posting. See here: http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline . It seems to work most of the time for me, but doesn't always work, apparently. – Tyler Jun 15 '14 at 18:43

1 Answers1

2

As confirmed in the comments/chat, the problem is not that the thread 3 not printing any output but the output being buffered by printf().

You could use fflush() to flush it, or use \n to flush stdout as it's usually line-buffered.

Or you can disable the buffering altogether by using setbuf().

setbuf(stdout, NULL);
P.P
  • 117,907
  • 20
  • 175
  • 238