race condition (sort of)? message box verus function return

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

race condition (sort of)? message box verus function return

speartip
Hi, my message box is appearing before function return before it...







Code:

cout<<"Call_SDL_GetCurrentDisplayMode"<     call_getCurrentDisplayMode(horizontal, vertical, &dm);
    cout<< horizontal << "\t"<< vertical << '\n';

    MessageBox(0,"ScreenTest", "Ready?", MB_OK);


Well screentest pops up, and when I press "Ok", the horizontal and vertical are then printed. I can live with it -- jut wanted to know whether this is a permanent condition or whether there wa a workaround.

Thx

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

Evan Ramos
Try replacing the '\n' with std::endl, or inserting std::flush after the '\n'.

-H
-----
"After you finish the first 90% of a project, you have to finish the
other 90%." - Michael Abrash
_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

speartip
In reply to this post by speartip
Hi,

Thank you for the solutions, they all worked. Of all the combinations I tried, the one below worked the best:








Code:

std::cout<< horizontal <<"\t"<


Generally speaking, why did flushing the stream and combinations, etc., work to speed up the routine?

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

Jonathan Dearborn-2
It doesn't speed anything up.  There's also no race condition, because these commands are all done on a single thread.

Output streams write to a memory buffer.  That memory buffer needs to be flushed so that it is actually sent to the output device (which could be anything, not just a simple terminal).  C and C++ separate writing and flushing to give you more control.  If you don't care about that control, just use std::endl to write a newline and force a flush.

Jonny D


On Mon, Feb 13, 2017 at 12:24 PM, speartip <[hidden email]> wrote:
Hi,

Thank you for the solutions, they all worked. Of all the combinations I tried, the one below worked the best:




Code:


std::cout<< horizontal <<"\t"<




Generally speaking, why did flushing the stream and combinations, etc., work to speed up the routine?

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

speartip
In reply to this post by speartip
Hi, points well taken thanks.

What I meant by sped up was that now my cout information was arriving before by message box where it had not before.

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

Rainer Deyke-2
In reply to this post by speartip
On 13.02.2017 18:24, speartip wrote:
> std::cout<< horizontal <<"\t"<<std::flush<< vertical << "\t"<<std::flush<<endl;

'std::cout << std::endl' is equivalent to 'std::cout << '\n' <<
std::flush', so you're flushing three times here.  That's a bad idea
because flushing slows down the program.  I recommend this:

   std::cout << horizontal << "\t" << vertical << "\t\n" << std::flush;

Flush exactly once, at the very end of your output, and always use
'std::flush' instead of 'std::endl' because 'std::flush is more explicit
about your intention.


--
Rainer Deyke - [hidden email]

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

speartip
In reply to this post by speartip







Quote:
Flush exactly once, at the very end of your output, and always use
'std::flush' instead of 'std::endl' because 'std::flush is more explicit
about your intention.


Yes, following that advice, that's true. Makes more sense when I look at the changes.







Quote:
That's a bad idea
because flushing slows down the program.


So what's the best balance in a speed optimized routine?

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

Jonathan Dearborn-2
For optimizing speed, do not print anything at all. :)

Jonny D


On Wed, Feb 15, 2017 at 10:00 AM, speartip <[hidden email]> wrote:



Quote:

Flush exactly once, at the very end of your output, and always use
'std::flush' instead of 'std::endl' because 'std::flush is more explicit
about your intention.




Yes, following that advice, that's true. Makes more sense when I look at the changes.



Quote:

That's a bad idea
because flushing slows down the program.




So what's the best balance in a speed optimized routine?

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: race condition (sort of)? message box verus function return

speartip
In reply to this post by speartip
Idea

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Loading...