0

Im trying to print some error messages in my code, and I need to show the error immediately when they appear, Using it like this works :

> cout << "erroe msg" << endl;

But with cerr the print isnt consestent

> cerr<< "error msg" << endl;

Isnt endl on stderr flush the buffer the same way it dose with stdout ?

minimal example :

int uthread_block(int tid){
    block_alarm();
    if(tid == 0){
        cerr <<"thread library error: You cannot block the main thread\n" << endl;
        return -1;
    } 

This function is used sometimes while going throw 100 thread in my program and sould be print an error message in a specific time. If ill change cerr to cout it will be correct at the time, and with cerr it can be show at the start of the output the end or the middle ( probable when it flushing the buffer)

limitless
  • 669
  • 7
  • 18
  • 2
    `cerr` is unit-buffered anyway; i.e. it will be flushed at every write operation. What makes you think it isn't being? Please provide a full [mcve] with details of how you are running your program. – BoBTFish Apr 19 '17 at 14:51
  • 3
    Please define "the print isnt consestent" – Slava Apr 19 '17 at 14:52
  • the print of the message can be at the start of the output the end or the middle, but when changing to cout it in the right place – limitless Apr 19 '17 at 15:07
  • 1
    Are you sure that it's not just a threading issue, because if your other threads are outputting then the order things will be printed isn't defined unless you've put mutexes etc in – UKMonkey Apr 19 '17 at 15:12
  • The other thread are printing, but what makeing me think it is in the cerr is because im just changing it and working as expected. (And btw the threads printing is a tester that should work). – limitless Apr 19 '17 at 15:24
  • You haven't demonstrated the problem. This may have some helpful info http://stackoverflow.com/questions/6374264/is-cout-synchronized-thread-safe – Kenny Ostrom Apr 19 '17 at 15:41

0 Answers0