<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual C++ Tips &#187; multithreaded console application</title>
	<atom:link href="http://weseetips.com/tag/multithreaded-console-application/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Use printf() while tracing from multithreaded console applications.</title>
		<link>http://weseetips.com/2008/06/02/use-printf-while-tracing-from-multithreaded-console-applications/</link>
		<comments>http://weseetips.com/2008/06/02/use-printf-while-tracing-from-multithreaded-console-applications/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 17:09:10 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[cout]]></category>
		<category><![CDATA[cout.flush()]]></category>
		<category><![CDATA[flush()]]></category>
		<category><![CDATA[multithreaded console application]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[trace]]></category>
		<category><![CDATA[trace problem]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=152</guid>
		<description><![CDATA[Usually most of our back end server applications might be multi-threaded console applications which contain several threads running simultaneously. Usually we do trace issues by printing strings to console screen by using cout or printf(). As a general practice, we usually stick upon cout because of our affinity towards C++. But there is a great [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
Usually most of our back end server applications might be multi-threaded console applications which contain several threads running simultaneously. Usually we do trace issues by printing strings to console screen by using <span style="color:#0000ff;">cout </span>or <span style="color:#0000ff;">printf()</span>. As a general practice, we usually stick upon <span style="color:#0000ff;">cout </span>because of our affinity towards C++. <img src='http://weseetips.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  But there is a great pitfall lies behind.</p>
<p><img class="alignnone size-medium wp-image-15" src="http://weseetips.files.wordpress.com/2008/03/icon_underthehood.jpg?w=208" alt="" width="208" height="32" /><br />
If you use <span style="color:#0000ff;">cout</span>, you can see the <span style="color:#0000ff;">traces are shuffled and displayed only after certain interval.</span> This is because, <span style="color:#0000ff;">cout uses buffered output</span>. So if you need real time tracing, use <span style="color:#0000ff;">printf()</span>.</p>
<p>if you still want to use cout, then flush the streams by calling flush(). See the code snippt.</p>
<pre>// Trace the message.
cout &lt;&lt; _T( "Tracing from thread" );

// flush the buffer.
cout.flush();</pre>
<p><img class="alignnone size-medium wp-image-18" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
This difference may not be visible if you are running one thread at a time. If you have a number of threads and each one is busy in dumping traces, definitely you can see this. <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-53" src="http://weseetips.files.wordpress.com/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/06/02/use-printf-while-tracing-from-multithreaded-console-applications/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

