<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Cpp</title>
        <link>http://nimblecoder.com/blog/category/5.aspx</link>
        <description>C and C++</description>
        <language>en-US</language>
        <copyright>Ryan Van Slooten</copyright>
        <generator>Subtext Version 2.1.1.1</generator>
        <item>
            <title>Death of single-threaded development?</title>
            <link>http://nimblecoder.com/blog/archive/2007/06/08/Death-of-single-threaded-development.aspx</link>
            <description>&lt;p&gt;I was reading my email when I came across this blog about the &lt;a href="http://blogs.zdnet.com/Ou/?p=519"&gt;death of single-threaded development&lt;/a&gt;. Now I've written many multi-threaded applications in C++ for applications, communications, and graphical displays, but I don't see the death of single-threaded development any time soon. I even wrote a paper while I was at university about how multi-threaded programming should be introduced in the curriculum much earlier. At the very least, the multi-threaded thought process should be taught but I don't see that happening.&lt;/p&gt; &lt;p&gt;Samples are usually distributed as single-threaded applications because they are easier to understand. Unfortunately people replicate the same style and methodology in their own applications. Until sample applications are distributed as multi-threaded applications, I do not believe single-threaded application will die or even that multi-threaded applications will outnumber single-threaded applications.&lt;/p&gt; &lt;h3&gt;My First "Real" (as in Commercial) Multi-Threaded Experience&lt;/h3&gt; &lt;p&gt;Personally, I'd love for more people to use multi-threading in applications. Unfortunately there is a significant learning curve where you progress from novice to expert. I remember one of my first multi-threaded applications involved reading communications data from a device asynchronously as the display updated the values. During QA, a bug was found if you clicked this dialog and pressed that button in just the right (or wrong) sequence you could crash the application. It turned out to be an issue where I forgot to suspend the communications thread before changing some key values. Actually, it wouldn't have crashed if there had been a semaphore in the communications thread to protect the resizing of an array.&lt;/p&gt; &lt;h3&gt;The Classic Case of Debugging Multi-Threaded Applications&lt;/h3&gt; &lt;p&gt;Another multi-threaded issue I had was a communications driver that has to run 24x7 on an embedded controller. There were four-&lt;em&gt;plus&lt;/em&gt; threads: one for the application, one to monitor and handle configuration changes, one for the TCP/IP source data, and one for each of the configured serial ports. The source data thread read data from the host and then allow the serial ports to read the data. During the update of the source data, I protected the data with a class modeled after the Delphi &lt;em&gt;TMultiReadExclusiveWriteSynchronizer&lt;/em&gt;. The application would mysteriously lock up after about 7 days and it was a particularly stubborn. Debugging crashes that occur only every 7 days takes a little while to find and fix. I suspected it was an issue with my scheduler (the component that controlled how quickly the threads execute or resume after completing an iteration). There was an issue with the scheduler early in the development where the tick counter could roll over (a &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/gettickcount.asp"&gt;DWORD holding milliseconds&lt;/a&gt; can count up to 49.7 days), but I knew that could be an issue from the beginning and fixed that quickly. After a good deal of debugging, I found the issue was with my &lt;em&gt;TMultiReadExclusiveWriteSynchronizer&lt;/em&gt; class. I wrote a stress test application which hammered the synchronizer and sure enough found the problem. The application is still in production and running in many different plants around the US.&lt;/p&gt; &lt;h3&gt;The Purpose or Need of Multi-Threading&lt;/h3&gt; &lt;p&gt;In the case of the &lt;a href="http://blogs.zdnet.com/Ou/?p=519"&gt;blog article&lt;/a&gt; mentioned at the beginning of this post, the author is promoting multi-threading for graphic and game algorithms but I believe it is broader than just graphic algorithms. I get frustrated when Outlook doesn't respond when it is downloading messages. Programming multi-threaded applications makes you a better programmer and a better debugger -- that is if you are successful at it. Some programmers are just not cut out for multi-threading applications.&lt;/p&gt; &lt;p&gt;Many web applications are moving to AJAX to provide a more seemless experience without a bunch of page loads. Unfortunately, some tasks still take a long time and should be handled differently. I modified a JobHandler (based on a &lt;a href="http://www.devx.com/asp/Article/29617"&gt;DevX article&lt;/a&gt;) to support long-running tasks (BizTalk orchestrations, web services, communications, etc.). I added an Abort function, a watchdog timer, and general information like start and finish time, Session ID, and user name. In web-based applications, usually you must poll the JobHandler to see if it is complete. In OS-based applications, you can use a notifier or callback function to avoid polling.&lt;/p&gt; &lt;p&gt;There are typically two reasons for multi-threading: to complete a task quicker and to provide a seemless response. The first reason is most compelling due to the enormous performance gains that can be achevied. It is also the more difficult case to design more since you have to divide the tasks or calculations across multiple threads. The second case usually doesn't increase performance significantly, but the user appreciates the application more when it isn't frozen and not updating. At least in this case, you can monitor the thread, provide feedback, and attempt to abort it if necessary.&lt;/p&gt; &lt;h3&gt;Common Issues with Multi-Threading&lt;/h3&gt; &lt;ol&gt; &lt;li&gt;Deadlocks -- classic issue, but also includes handling "hung" threads and using watchdog timers&lt;/li&gt; &lt;li&gt;Scheduling threads -- figuring out how to run the threads quickly or pause the threads without overloading the system&lt;/li&gt; &lt;li&gt;Notifications -- notifying the application or external systems about status or progress&lt;/li&gt;&lt;/ol&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;Multi-theading is broader than just game and graphics programming, although the need for multi-threading in those areas is significant and absolute. Multi-threading increases your ability and experience, although you must expect challenges along the way.&lt;/p&gt;&lt;img src="http://nimblecoder.com/blog/aggbug/28.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ryan Van Slooten</dc:creator>
            <guid>http://nimblecoder.com/blog/archive/2007/06/08/Death-of-single-threaded-development.aspx</guid>
            <pubDate>Fri, 08 Jun 2007 16:08:01 GMT</pubDate>
            <wfw:comment>http://nimblecoder.com/blog/comments/28.aspx</wfw:comment>
            <comments>http://nimblecoder.com/blog/archive/2007/06/08/Death-of-single-threaded-development.aspx#feedback</comments>
            <wfw:commentRss>http://nimblecoder.com/blog/comments/commentRss/28.aspx</wfw:commentRss>
            <trackback:ping>http://nimblecoder.com/blog/services/trackbacks/28.aspx</trackback:ping>
        </item>
        <item>
            <title>Programming Oddities sizeof</title>
            <link>http://nimblecoder.com/blog/archive/2007/02/22/Programming-Oddities-sizeof.aspx</link>
            <description>&lt;p&gt;One of the strangest programming concepts that I came across was working with an embedded processor, Analog Devices SHARC ADSP-21062, on a project. I was shocked when I found out the following:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;sizeof&lt;/span&gt;(&lt;span class="kwrd"&gt;char&lt;/span&gt;) == &lt;span class="kwrd"&gt;sizeof&lt;/span&gt;(&lt;span class="kwrd"&gt;short&lt;/span&gt;) == &lt;span class="kwrd"&gt;sizeof&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt;) == 1&lt;/pre&gt;
&lt;p&gt;I didn't think this was possible, but sure enough it was. After reviewing the C standard I realized that this was allowed and the only requirement was: sizeof(char) &amp;lt;= sizeof(short) &amp;lt;= sizeof(int).&lt;/p&gt;
&lt;p&gt;This actually had a profound affect on a later project using the same processor family when I was porting an embedded GUI, &lt;a href="http://www.microwindows.org/"&gt;Nano-X&lt;/a&gt; or formerly microwindows, to that processor. The issue was mainly with assumptions on casting to (char). This affected several areas in the source code as well as the fonts. The assumption was that casting to (char) automatically bit-masked the value to 8-bits:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;
result = (&lt;span class="kwrd"&gt;char&lt;/span&gt;) &lt;span class="kwrd"&gt;value&lt;/span&gt;;    &lt;span class="rem"&gt;// assumed 8-bit result&lt;/span&gt;
result = &lt;span class="kwrd"&gt;value&lt;/span&gt; &amp;amp; 0x00FF;  // &lt;span class="kwrd"&gt;explicit&lt;/span&gt; bitmask&lt;/pre&gt;
&lt;p&gt;The solution was to locate all casts and determine the impact and convert them to casts. I ended up using a preprocessor macro to maintain compatibility and speed where it would use the cast on normal operating systems and processors but use the bitmask on the Analog Devices processor.&lt;/p&gt;&lt;img src="http://nimblecoder.com/blog/aggbug/11.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ryan Van Slooten</dc:creator>
            <guid>http://nimblecoder.com/blog/archive/2007/02/22/Programming-Oddities-sizeof.aspx</guid>
            <pubDate>Thu, 22 Feb 2007 15:35:05 GMT</pubDate>
            <wfw:comment>http://nimblecoder.com/blog/comments/11.aspx</wfw:comment>
            <comments>http://nimblecoder.com/blog/archive/2007/02/22/Programming-Oddities-sizeof.aspx#feedback</comments>
            <wfw:commentRss>http://nimblecoder.com/blog/comments/commentRss/11.aspx</wfw:commentRss>
            <trackback:ping>http://nimblecoder.com/blog/services/trackbacks/11.aspx</trackback:ping>
        </item>
        <item>
            <title>Visual Floating Point Debugger Tool</title>
            <link>http://nimblecoder.com/blog/archive/2007/02/07/visual-floating-point-debugger-tool.aspx</link>
            <description>&lt;p&gt;When I was debugging communication protocols a long time ago, I would often have to convert a 32-bit integer into a 32-bit floating point number. This did not mean using &lt;font face="Courier New"&gt;atof&lt;/font&gt;, &lt;font face="Courier New"&gt;fscanf&lt;/font&gt;, &lt;font face="Courier New"&gt;_fcvt&lt;/font&gt; or similar function, but rather interpretting the bits as an &lt;a href="http://en.wikipedia.org/wiki/IEEE_754"&gt;IEEE 754&lt;/a&gt; or &lt;font face="Arial"&gt;IEC 60559:1989 &lt;/font&gt;floating point number. An example would be:&lt;/p&gt;
&lt;p&gt;1.0 == 0x3F800000 (1065353216)&lt;/p&gt;
&lt;p&gt;In the process, I wrote a tool that displays the components of the floating point number and allows you to change the raw hexadecimal bits. Here is a screen shot:&lt;/p&gt;
&lt;p&gt;&lt;img width="296" height="474" align="middle" src="/blog/images/nimblecoder_com/blog/FloatScreenShot01.PNG" alt="screen shot" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;This tool was written with VB6 however I have always wanted to make a C++/WTL version of the tool. Maybe I will get around to it sometime. &lt;/font&gt;&lt;font face="Arial"&gt;&lt;a href="http://www.codeproject.com/dotnet/ExtremeFloatingPoint1.asp"&gt;Here&lt;/a&gt; is a decent article describing the floating point format.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nimblecoder.com/blog/Images/nimblecoder_com/blog/Float.zip"&gt;Float Tool Source&lt;/a&gt;: 18KB&lt;/p&gt;&lt;img src="http://nimblecoder.com/blog/aggbug/10.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ryan Van Slooten</dc:creator>
            <guid>http://nimblecoder.com/blog/archive/2007/02/07/visual-floating-point-debugger-tool.aspx</guid>
            <pubDate>Wed, 07 Feb 2007 19:10:43 GMT</pubDate>
            <wfw:comment>http://nimblecoder.com/blog/comments/10.aspx</wfw:comment>
            <comments>http://nimblecoder.com/blog/archive/2007/02/07/visual-floating-point-debugger-tool.aspx#feedback</comments>
            <wfw:commentRss>http://nimblecoder.com/blog/comments/commentRss/10.aspx</wfw:commentRss>
            <trackback:ping>http://nimblecoder.com/blog/services/trackbacks/10.aspx</trackback:ping>
        </item>
    </channel>
</rss>