Click here to Skip to main content
15,891,867 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionUnexpected sequence of cout / perror in console output Pin
Vaclav_10-Mar-19 7:23
Vaclav_10-Mar-19 7:23 
AnswerRe: Unexpected sequence of cout / perror in console output Pin
Richard MacCutchan10-Mar-19 22:52
mveRichard MacCutchan10-Mar-19 22:52 
GeneralRe: Unexpected sequence of cout / perror in console output Pin
k505411-Mar-19 2:28
mvek505411-Mar-19 2:28 
GeneralRe: Unexpected sequence of cout / perror in console output Pin
Richard MacCutchan11-Mar-19 3:56
mveRichard MacCutchan11-Mar-19 3:56 
GeneralRe: Unexpected sequence of cout / perror in console output Pin
k505411-Mar-19 5:41
mvek505411-Mar-19 5:41 
GeneralRe: Unexpected sequence of cout / perror in console output Pin
Richard MacCutchan11-Mar-19 5:52
mveRichard MacCutchan11-Mar-19 5:52 
GeneralRe: Unexpected sequence of cout / perror in console output Pin
k505411-Mar-19 11:59
mvek505411-Mar-19 11:59 
GeneralRe: Unexpected sequence of cout / perror in console output Pin
leon de boer11-Mar-19 18:10
leon de boer11-Mar-19 18:10 
Really for the OP but I will put here to follow the discussion

Someone is on a multicore processor and the streams which are buffering are on different cores with resource locks Smile | :)

This is generally not guaranteed to give the expected order
printf("Expect 1\n");
perror("Expect 2\n");
printf("Expect 3\n");

Simply put two independent streams are not guaranteed to synchronize without you forcing the issue.
In the above Expect1 will always be before Expect3 but Expect2 can be before, in the middle or after those two lines.

sync_with_stdio() if implemented will work but it implies a heavy overhead on a many multicore situation which is why it might be off. I think it is supposed to default on with some c++ versions (but on multicore it is often violated because it is horrific) you would need to check with your compiler.

The simple answer is stop using perror and simply just use one stream aka printf/cout, flush after each print or put lock primitives in the order to guarantee the order.

It is easily avoidable ... so avoid it Smile | :)
In vino veritas


modified 12-Mar-19 1:36am.

QuestionC Programming[Solved] Pin
Evolt_Pratom9-Mar-19 1:07
Evolt_Pratom9-Mar-19 1:07 
AnswerRe: C Programming Pin
Richard MacCutchan9-Mar-19 1:27
mveRichard MacCutchan9-Mar-19 1:27 
GeneralRe: C Programming Pin
k50549-Mar-19 4:27
mvek50549-Mar-19 4:27 
GeneralRe: C Programming Pin
Richard MacCutchan9-Mar-19 5:16
mveRichard MacCutchan9-Mar-19 5:16 
GeneralRe: C Programming Pin
Evolt_Pratom10-Mar-19 3:42
Evolt_Pratom10-Mar-19 3:42 
GeneralRe: C Programming Pin
Richard MacCutchan10-Mar-19 7:02
mveRichard MacCutchan10-Mar-19 7:02 
Questiontemplate confusion Pin
Rw2377-Mar-19 13:00
Rw2377-Mar-19 13:00 
AnswerRe: template confusion Pin
Rw2378-Mar-19 2:37
Rw2378-Mar-19 2:37 
GeneralRe: template confusion Pin
Richard MacCutchan8-Mar-19 4:01
mveRichard MacCutchan8-Mar-19 4:01 
GeneralRe: template confusion Pin
Victor Nijegorodov8-Mar-19 6:21
Victor Nijegorodov8-Mar-19 6:21 
GeneralRe: template confusion Pin
Rw2379-Mar-19 6:08
Rw2379-Mar-19 6:08 
GeneralRe: template confusion Pin
k50548-Mar-19 6:50
mvek50548-Mar-19 6:50 
GeneralRe: template confusion Pin
Rw2379-Mar-19 5:57
Rw2379-Mar-19 5:57 
QuestionAero snap with CDockablePane Pin
jung-kreidler7-Mar-19 3:51
jung-kreidler7-Mar-19 3:51 
AnswerRe: Aero snap with CDockablePane Pin
_Flaviu12-Mar-19 22:49
_Flaviu12-Mar-19 22:49 
GeneralRe: Aero snap with CDockablePane Pin
jung-kreidler12-Mar-19 23:31
jung-kreidler12-Mar-19 23:31 
QuestionHow to run the C++ app on cluster? Pin
oleg635-Mar-19 6:39
professionaloleg635-Mar-19 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.