5

I have a C++ program that contains several cout statements. I have ensured that all of them end with an endl. My problem is that the program seldom halts until a user presses Enter (so I assume that the output buffer isn't always flushed as it should). Pressing enter resumes program execution. This is quite problematic as my program takes several hours to execute, so I can't afford to press enter all the time! Note that sometimes the program halts after a minute and other times after more than an hour.

Here's a small code snippet:

for(int i = 0; i < _numIterations; i++){

    std::cout << "Iteration " << i << std::endl;

    // Computations and more print statements.
}

Note that I use Theano through embed Python, and that my Python code also contains print statements. My Python code only calls print, not sys.stdout.flush() after each print. However, it is rare that the program execution hangs after a Python-generated print statement. Have I missed something obvious? Should I make a call to sys.stdout.flush() in the Python code? Unfortunately, I cannot provide more code as my program consists of dozens of classes.

[Edit] I've paused the program with a debugger when it was hanging, and no source was available to be displayed. The call stack was: enter image description here

It seems that a thread is waiting. However, I have not set up these threads myself. They were either generated by Cuda or the Havok physics engine that I'm also using. I'll investigate.

Bennie
  • 51
  • 2
  • Is somebody calling `std::cin` or asking for input somewhere? – 1201ProgramAlarm Dec 16 '15 at 05:55
  • Right, I wanted to mention that and I forgot. I do call it, but only once at the very end of the program, after the main execution loop. – Bennie Dec 16 '15 at 05:57
  • 2
    Have you tried breaking into it with a debugger when it hits one of these pauses to see what it is up to? – 1201ProgramAlarm Dec 16 '15 at 06:00
  • Is your program multi-threaded? – paddy Dec 16 '15 at 06:03
  • I can't believe I haven't done that already. That's what happens when I focus too much on the core of the code. I'll get back to you when it happens again. Thanks! – Bennie Dec 16 '15 at 06:07
  • @Paddy, Yes, it's multithreaded through the Havok physics engine. All the multithreaded calls are handled internally by Havok, and my couts are in the main thread. – Bennie Dec 16 '15 at 06:08
  • Hmmm, I was just wondering whether it might be hitting your `cin` section before all threads are finished executing. Do you do anything to join all threads to the parent prior to exiting? – paddy Dec 16 '15 at 06:10
  • I don't join the threads because Havok is mostly a black box. However, I shutdown Havok before calling cin, so I'm fairly sure it's not hit until the very end. Besides, the program pauses several times during execution and I only have one cin in main. – Bennie Dec 16 '15 at 06:14
  • @1201ProgramAlarm I've used a debugger and edited my initial post. – Bennie Dec 16 '15 at 15:32
  • @Bennie: That's surely not the call stack for the main thread? It looks like some sort of CUDA-related worker thread. – Cameron Dec 16 '15 at 23:21
  • Indeed. I'm just saying that I don't think the error is caused by cin in the main thread. – Bennie Dec 16 '15 at 23:56
  • That call stack does not look like your main thread, and it looks like something waiting to free memory. Look at the call stacks for the other threads. Maybe something there can clue you in. – 1201ProgramAlarm Dec 17 '15 at 02:56
  • I agree, I never said the main thread seemed to be the issue. I hope I can find the problem without having access to the thread generating source code. That said, would you have an idea as to why pressing Enter releases the thread? That's very odd if you ask me. – Bennie Dec 17 '15 at 15:24
  • If you haven't already, could you try disabling iostream synchronization with stdio via a call to `std::ios_base::sync_with_stdio(false);` at the beginning of your program and see if it still hangs? I'm wondering if there's a sync issue with sending output to the standard output. – Vijay Varadan Feb 11 '16 at 17:32

0 Answers0