<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Being Cellfish : C++</title><link>http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx</link><description>Tags: C++</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Exceptions or not?</title><link>http://blogs.msdn.com/cellfish/archive/2009/10/01/exceptions-or-not.aspx</link><pubDate>Fri, 02 Oct 2009 08:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9902199</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9902199.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9902199</wfw:commentRss><description>&lt;P&gt;I've always had mixed feelings for exceptions. First of all exceptions should only be used for exceptional things, not things that are expected. For example if you open a file it may fail for several reasons; it does not exists, you do not have rights to open it or it may be locked. These are all perfectly reasonable errors and throwing an exception for these failures is in my opinion not a great API design. If there are other methods to check for all these circumstances it might be OK but not optimal. A reasonable compromise is to have a TryOpen method which never throws an exception. This gives the user of the API&amp;nbsp;the option of letting file open errors being expected or fatal.&lt;/P&gt;
&lt;P&gt;Because exceptions should only be thrown for exceptional circumstances there should be no reason to catch them other in the application's main loop unless you can add information to the error in which case you want to wrap and throw a better exception. The exception to this rule is when you use an API (such as file open mentioned before) that throws for pretty common error cases you want to ignore.&lt;/P&gt;
&lt;P&gt;Another thing I don't like with exceptions is the fact that you (in C++ and C#, Java is better here) get no help from the compiler to know what types of exceptions a function may call. So in C++ and C# you may think you catch everything you need but you really don't which leads to unwanted behavior in your application. It is just too easy to get into a situation where you think a number of lines are going to be executed but they're not for some exception.&lt;/P&gt;
&lt;P&gt;However if you use return codes (as you would in a C program) you get your code cluttered with "if (previous called failed) handle error" which hides what the code really does. This can be hidden using macros to handle errors (ex: result = DoSomething ON_FAIL_RETURN(result); ). And if there is no error handling it is hard to know what happens when the next line is executed even though there was an error.&lt;/P&gt;
&lt;P&gt;Using exceptions is also a kind of "I failed and I hope somebody else somewhere can handle it" while the use of return codes defines a clear contract "I failed and you who called me must handle it". So even though exceptions are convenient, return codes feel more defensive, i.e. are easier to get right resulting in less bugs but sometimes at the cost of less clear code. As usual the mix of both approaches is probably the best. But there are more alternatives. If you use &lt;A href="http://blog.objectmentor.com/articles/2009/01/05/abstracting-away-from-exceptions" target=_blank mce_href="http://blog.objectmentor.com/articles/2009/01/05/abstracting-away-from-exceptions"&gt;adverse as described by Feathers&lt;/A&gt; I'm prepared to accept exceptions to a much larger degree because they're being handled immediately so it reminds of the return code approach.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9902199" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Java/default.aspx">Java</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/.Net/default.aspx">.Net</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Development/default.aspx">Development</category></item><item><title>Remember to allocate memory just in case you run out of memory</title><link>http://blogs.msdn.com/cellfish/archive/2009/08/10/remember-to-allocate-memory-just-in-case-you-run-out-of-memory.aspx</link><pubDate>Tue, 11 Aug 2009 07:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9863808</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9863808.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9863808</wfw:commentRss><description>&lt;P&gt;Today I read this &lt;A href="http://blogs.msdn.com/tess/archive/2009/08/10/why-do-i-see-executionengineexception-stackoverflowexception-and-outofmemoryexception-on-the-heap-when-debugging-net-applications.aspx" target=_blank mce_href="http://blogs.msdn.com/tess/archive/2009/08/10/why-do-i-see-executionengineexception-stackoverflowexception-and-outofmemoryexception-on-the-heap-when-debugging-net-applications.aspx"&gt;blog post&lt;/A&gt; about how all .Net applications create three exceptions upon start; System.ExecutionEngineException, System.StackOverflowException and System.OutOfMemoryException. The reason is that if these are not allocated from the start, they cannot be created when they occur. This reminded me of when I was writing high performance &lt;A href="http://en.wikipedia.org/wiki/Daemon_(computer_software)" target=_blank mce_href="http://en.wikipedia.org/wiki/Daemon_(computer_software)"&gt;daemons&lt;/A&gt; in C a few years ago.&lt;/P&gt;
&lt;P&gt;In those daemons I did a number of things to prepare for the event of memory starvation. First of all I allocated some memory (a few Kb if I remember correctly). If the daemon ran out of memory it was&amp;nbsp;suspended for up to a second and the memory allocation was tried again. Typically I did a handful of retries and if they all failed I released the preallocated memory, logged the error and terminated the process.&lt;/P&gt;
&lt;P&gt;The reasoning behind this strategy was based on three assumptions. First of all, if a process cannot allocate memory it is because some other process is leaking memory (or is just using a lot of memory). Second assumption is that the offending process will terminate fairly quickly because of this. Last assumption is that if the offending process does not release memory quick enough it might be the current process that is the problem so it must terminate to let other processes proceed normally. But before termination we release some preallocated memory so that we have enough memory to log the problem.&lt;/P&gt;
&lt;P&gt;So what about multi-threaded daemons? If one thread releases some memory another might take it all before we get the chance to log the problem. Well, in my case all daemons were single threaded. If I needed "threads" (which was rare) I &lt;A href="http://en.wikipedia.org/wiki/Fork_(software_development)" target=_blank mce_href="http://en.wikipedia.org/wiki/Fork_(software_development)"&gt;forked&lt;/A&gt;. So that was not a problem I had to consider.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9863808" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/.Net/default.aspx">.Net</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category></item><item><title>A reminder if you want to read/write your file in non blocking mode (in C(++))</title><link>http://blogs.msdn.com/cellfish/archive/2009/04/16/a-reminder-if-you-want-to-read-write-your-file-in-non-blocking-mode-in-c.aspx</link><pubDate>Thu, 16 Apr 2009 14:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9552063</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9552063.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9552063</wfw:commentRss><description>&lt;P&gt;Sometimes you might want to read a file &lt;A href="http://en.wikipedia.org/wiki/Non-blocking_I/O" target=_blank mce_href="http://en.wikipedia.org/wiki/Non-blocking_I/O"&gt;non-blocking&lt;/A&gt;. It could be &lt;A href="http://en.wikipedia.org/wiki//dev/random" target=_blank mce_href="http://en.wikipedia.org/wiki//dev/random"&gt;/dev/random&lt;/A&gt; because waiting for entropy might take very long. Also when you have to read device files on unix you sometimes have to read them non-blocking. A common pattern is to open the file non-blocking using the &lt;A href="http://linux.die.net/man/2/open" target=_blank mce_href="http://linux.die.net/man/2/open"&gt;open&lt;/A&gt; method: open(..., O_NONBLOCK, ...)&lt;/P&gt;
&lt;P&gt;This however may be a problem. This type of open will typically succeed (because the system is opening the file non-blocking) and when you then start reading (or writing) you get a failure. The failure can be anything from permission problems to unavailable devices. This is how non-blocking I/O works and something you're well aware of if you're using sockets for communication but since "this is files" you might forget to take non-blocking into account.&lt;/P&gt;
&lt;P&gt;So there is a really easy way to work around this. Files, unlike network sockets completes the open operation very fast. So making the open call non-blocking generally does not make so much sense. Instead I suggest you open the file in blocking mode and then change it to non-blocking one opened. And this is how you do that:&lt;/P&gt;
&lt;P&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;PRE&gt;&lt;DIV class=csharpcode&gt;
&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; fd = open(..., 0, ...); &lt;SPAN class=rem&gt;// Filed open in blocking&lt;/SPAN&gt;
&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; fd_flags = fcntl(fd, F_GETFL);
&lt;SPAN class=lnum&gt;   3:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (fd_flags &amp;lt; 0) { &lt;SPAN class=rem&gt;// Get any flags set on the fd.&lt;/SPAN&gt;
&lt;SPAN class=lnum&gt;   4:  &lt;/SPAN&gt;    close(fd);
&lt;SPAN class=lnum&gt;   5:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; error;
&lt;SPAN class=lnum&gt;   6:  &lt;/SPAN&gt;}
&lt;SPAN class=lnum&gt;   7:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (-1 == fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK)) { &lt;SPAN class=rem&gt;// Add non blocking to the fd flags&lt;/SPAN&gt;
&lt;SPAN class=lnum&gt;   8:  &lt;/SPAN&gt;    close(fd);
&lt;SPAN class=lnum&gt;   9:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; error;
&lt;SPAN class=lnum&gt;  10:  &lt;/SPAN&gt;}
&lt;/DIV&gt;&lt;/PRE&gt;
&lt;P&gt;But there is (at least) one situation where a non-blocking open might make some sense. That is when you open a file on a remote server and you cannot afford the waiting of an open call. And in this case you must treat the file descriptor like any other file descriptor used for network communication and use&amp;nbsp;&lt;A href="http://en.wikipedia.org/wiki/Select_(Unix)" target=_blank mce_href="http://en.wikipedia.org/wiki/Select_(Unix)"&gt;select&lt;/A&gt; to make sure the file descriptor is ready before you start using it.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9552063" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/_2A00_nix/default.aspx">*nix</category></item><item><title>Dangers of using Visual Studio 2008 Team System Code Coverage Tool for Native C++</title><link>http://blogs.msdn.com/cellfish/archive/2008/11/18/dangers-of-using-visual-studio-2008-team-system-code-coverage-tool-for-native-c.aspx</link><pubDate>Tue, 18 Nov 2008 16:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9080834</guid><dc:creator>cellfish</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9080834.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9080834</wfw:commentRss><description>&lt;P&gt;So now you know how to get coverage reports for native C++ using Visual Studio 2008 Team System (if not - &lt;A class="" title="Native C++ Code Coverage reports using Visual Studio 2008 Team System" href="http://blogs.msdn.com/cellfish/archive/2008/11/16/native-c-code-coverage-reports-using-visual-studio-2008-team-system.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/11/16/native-c-code-coverage-reports-using-visual-studio-2008-team-system.aspx"&gt;read this&lt;/A&gt;). There are a few things you need to know before you get excited. First of all the only metrics you get are line and block coverage. A block is basically a statement and each line typically consists of one or more blocks. Unless you have 100% coverage I think these metrics both are pretty useless when measuring quality. For example consider a function consisting of ten lines of code. There is an IF-statement checking for an error and it throws an exception if the error occurs. If the error never occurs during the test-run you still get 90% line coverage since the other nine lines are executed. I think this is pretty common in production code. Most of the code is for the common state and fewer lines are used to handle errors. So you get pretty high line coverage even if you do not test any of error cases.&lt;/P&gt;
&lt;P&gt;Block coverage is even worse. For example consider the following line:&lt;/P&gt;&lt;PRE&gt;SimpleClass* o = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; SimpleClass();&lt;/PRE&gt;
&lt;P&gt;That line produces two blocks of which only one is covered. And there is no reasonable way test the uncovered block since it probably has to do with when the process runs out of memory.&lt;/P&gt;
&lt;P&gt;Identifying functions that are not called at all is often considered an important part of the code coverage report. Here we have another problem with the visual studio tool. Functions never referenced by the code will be excluded from the report completely (I suspect this is the case since the linker will remove all unreferenced symbols as part of the optimization at link time). This means the following class will report 100% coverage it it is instantiated and only &lt;FONT face=courier&gt;GetA&lt;/FONT&gt; is called.&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; SimpleClass
{
&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;:
    SimpleClass(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; a, &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; b)
        : m_a(0) , m_b(b)
    { }

    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; GetA() { &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; m_a; }
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; GetB() { &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; m_b; }

&lt;FONT color=#0000ff&gt;private&lt;/FONT&gt;:
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; m_a;
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; m_b;
};&lt;/PRE&gt;
&lt;P&gt;So with all these potential problems there is another tool I'd recommend you consider. It's called &lt;A class="" href="http://www.bullseye.com/" target=_blank mce_href="http://www.bullseye.com/"&gt;BullsEye&lt;/A&gt;. It is a more "puristic" tool so there is no way to get block or line coverage, basically since those metrics are bad. In stead you can get Decision and Condition/Decision coverage. Basically Decision coverage checks that each conditional evaluates to both true and false and Condition/Decision coverage is when each part of a boolean expression is evaluated to both true and false. Consider the following line:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;if&lt;/FONT&gt;(a || b)&lt;/PRE&gt;
&lt;P&gt;There are two different decisions (either "a || b" is true or false) but four different conditions (both "a" and "b" must evaluate to true and false). BullsEye also adds instrumentation at compile time so the &lt;FONT face=courier&gt;GetB&lt;/FONT&gt; method in the example above will not be lost but be part of the report as an uncovered function even if not referenced anywhere in the code. In the initial example (ten lines with 90% line coverage) we would get 50% decision coverage which is a much better indicator of quality.&lt;/P&gt;
&lt;P&gt;And on using code coverage as a quality metric...I must insist you read &lt;A class="" href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx"&gt;one of my previous posts&lt;/A&gt; if you haven't done that already...&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9080834" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Tools/default.aspx">Tools</category></item><item><title>Native C++ Code Coverage reports using Visual Studio 2008 Team System</title><link>http://blogs.msdn.com/cellfish/archive/2008/11/16/native-c-code-coverage-reports-using-visual-studio-2008-team-system.aspx</link><pubDate>Sun, 16 Nov 2008 13:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9080305</guid><dc:creator>cellfish</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9080305.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9080305</wfw:commentRss><description>&lt;P&gt;The code coverage tool in Visual Studio 2008 Team System is quite easy to use from within the IDE unless you want code coverage for your native C++ code. In order to generate a code coverage report for native C++ you have to use the command line tools. This is how you do it:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;First of all your project must be compiled using the &lt;FONT face=courier&gt;/PROFILE&lt;/FONT&gt; link option. If you bring up your project properties it can be found here:&lt;BR&gt;&lt;FONT face=courier&gt;Configuration Properties -&amp;gt; Linker -&amp;gt; Advanced -&amp;gt; Profile&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;The profiler tools can then be found in the following directory:&lt;BR&gt;&lt;FONT face=courier&gt;C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;You need to add some instrumentation code to your EXE or DLL file and that is done with this command:&lt;BR&gt;&lt;FONT face=courier&gt;vsinstr.exe &amp;lt;YOUR_EXE_OR_DLL&amp;gt; /COVERAGE&lt;/FONT&gt;&lt;BR&gt;This will copy the original file to an ".orig"-file and create a new file with the original name that contains instrumentation code needed to gather coverage data.&lt;/LI&gt;
&lt;LI&gt;Now start the listener with this command:&lt;BR&gt;&lt;FONT face=courier&gt;VSPerfMon.exe /COVERAGE /OUTPUT:&amp;lt;REPORT_FILE_NAME&amp;gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;Now run your EXE or some test suite that uses the file you want to calculate coverage for.&lt;/LI&gt;
&lt;LI&gt;The listener started in step four (4) will not stop by it self once your test suite is finished so you have to stop in manually using this command (from a second command prompt):&lt;BR&gt;&lt;FONT face=courier&gt;VSPerfCmd.exe /SHUTDOWN&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;When the listener has stopped you just drag-n-drop the created ".coverage"-file into Visual Studio and you can view the results.&lt;/LI&gt;&lt;/OL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9080305" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Tools/default.aspx">Tools</category></item><item><title>C++ pointer basics for kids</title><link>http://blogs.msdn.com/cellfish/archive/2008/10/31/c-pointer-basics-for-kids.aspx</link><pubDate>Fri, 31 Oct 2008 10:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9026111</guid><dc:creator>cellfish</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9026111.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9026111</wfw:commentRss><description>&lt;P&gt;A little video to explain pointer basics in C++&amp;nbsp;to your kids... If you for some weird reason want to do that...&lt;/P&gt;
&lt;P align=center&gt;
&lt;OBJECT height=344 width=425&gt;&lt;PARAM NAME="movie" VALUE="http://www.youtube.com/v/UvoHwFvAvQE&amp;amp;hl=en&amp;amp;fs=1"&gt;&lt;PARAM NAME="allowFullScreen" VALUE="true"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;
&lt;embed src="http://www.youtube.com/v/UvoHwFvAvQE&amp;hl=en&amp;fs=1" mce_src="http://www.youtube.com/v/UvoHwFvAvQE&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9026111" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category></item><item><title>Sleep does not sleep for a specified period</title><link>http://blogs.msdn.com/cellfish/archive/2008/09/22/sleep-does-not-sleep-for-a-specified-period.aspx</link><pubDate>Mon, 22 Sep 2008 08:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8955498</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8955498.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8955498</wfw:commentRss><description>&lt;P&gt;I recently described how you can create &lt;A class="" title="Sleep less than one millisecond" href="http://blogs.msdn.com/cellfish/archive/2008/09/17/sleep-less-than-one-millisecond.aspx" target=_blank mce_href="http://blogs.msdn.com/cellfish/archive/2008/09/17/sleep-less-than-one-millisecond.aspx"&gt;your own usleep method&lt;/A&gt; when there isn't one to use. one thing that people however tend to forget is that the &lt;EM&gt;sleep methods&lt;/EM&gt; (sleep, usleep, nanosleep) only guarantees that the calling thread will be suspended for at least the given time. There is absolutely no guarantee that the thread will resume its execution&amp;nbsp;immediately after that time. On most platforms you don't really see the difference but there are exceptions. HPUX for example, even on an otherwise almost&amp;nbsp;idle system I typically experience a delay of a few milliseconds even though I &lt;EM&gt;sleep&lt;/EM&gt; for only a single microsecond.&lt;/P&gt;
&lt;P&gt;So if you want your thread to sleep for an exact amount of time you have use a different approach than sleep. Even if you&amp;nbsp;use some sort of synchronize mechanism (an event) and a timer (to trigger the event) there is no guarantee that the blocked thread will execute immediately when the event is triggered. This approach is more likely to work as expected than the use of sleep, but it is not fool proof since you never know what other processes execute on the system affecting your process' execution.&lt;/P&gt;
&lt;P&gt;The closest thing you can come to exact &lt;EM&gt;sleep&lt;/EM&gt; times is also the ugliest solution. You can always have a busy loop&amp;nbsp;checking if enough time has passed over and over again. This will however change the behavior of the sleep into a busy loop which is rarely a good solution to a problem.&lt;/P&gt;
&lt;P&gt;Since only a minimum time can ever be guaranteed with a sleep method, regardless of implementation you should not write your code so that it depends on an exact sleep method. I think you're actually addressing the wrong problem if you try getting an exact sleep. Instead try figuring out why you need an exact sleep and remove that reason because you'll never be able to get it to work exactly right. Maybe good enough for now but you never know what will happen in the future so it's just a bad design choice.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8955498" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category></item><item><title>Sleep less than one millisecond</title><link>http://blogs.msdn.com/cellfish/archive/2008/09/17/sleep-less-than-one-millisecond.aspx</link><pubDate>Wed, 17 Sep 2008 18:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8945066</guid><dc:creator>cellfish</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8945066.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8945066</wfw:commentRss><description>&lt;P&gt;On windows you have a problem you typically never encounter on Unix. That is how to get a thread to &lt;EM&gt;sleep&lt;/EM&gt; for less than one millisecond. On Unix you typically have a number of choices (sleep, usleep and nanosleep) to fit your needs. On windows however there is only &lt;EM&gt;Sleep&lt;/EM&gt; with millisecond granularity. You can however use the &lt;EM&gt;select&lt;/EM&gt; system call to create a microsecond sleep. On Unix this is pretty straight forward:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; usleep(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;long&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; usec)&lt;/FONT&gt;
&lt;FONT size=2&gt;{
&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;    struct&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; timeval tv;
    tv.tv_sec = usec/1000000L;
    tv.tv_usec = usec%1000000L;
&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;    return&lt;/FONT&gt;&lt;FONT size=2&gt; select(0, 0, 0, 0, &amp;amp;tv);
}&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;On windows however, the use of select forces you to include the winsock library which has to be initialized like this in your application:&lt;/P&gt;&lt;PRE&gt;&lt;FONT size=2&gt;    WORD wVersionRequested = MAKEWORD(1,0);
    WSADATA wsaData;
    WSAStartup(wVersionRequested, &amp;amp;wsaData);&lt;/PRE&gt;
&lt;P&gt;And then the select won't allow you to be called without any socket so you have to do a little more to create a microsleep method:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; usleep(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;long&lt;/FONT&gt;&lt;FONT size=2&gt; usec)
{
&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;    struct&lt;/FONT&gt;&lt;FONT size=2&gt; timeval tv;
    fd_set dummy;
    SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    FD_ZERO(&amp;amp;dummy);
    FD_SET(s, &amp;amp;dummy);
    tv.tv_sec = usec/1000000L;
    tv.tv_usec = usec%1000000L;
&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;    return&lt;/FONT&gt;&lt;FONT size=2&gt; select(0, 0, 0, &amp;amp;dummy, &amp;amp;tv);
}&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;All these created usleep methods return zero when successful and non-zero for errors.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8945066" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category></item><item><title>Mocks are not Stubs</title><link>http://blogs.msdn.com/cellfish/archive/2008/04/22/mocks-are-not-stubs.aspx</link><pubDate>Tue, 22 Apr 2008 09:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8416129</guid><dc:creator>cellfish</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8416129.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8416129</wfw:commentRss><description>&lt;P&gt;As many before me I was recently looking at different mocking frameworks in order to find one that suited my needs, and was written in C++. And there are not many alternatives out there if you're using C++. I've found &lt;A class="" title=MockPP href="http://mockpp.sourceforge.net/" target=_blank mce_href="http://mockpp.sourceforge.net/"&gt;one open source&lt;/A&gt; and two internal (Microsoft staff can access what can be described as an internal &lt;A class="" title=CodePlex href="http://codeplex.com/" target=_blank mce_href="http://codeplex.com/"&gt;codeplex&lt;/A&gt;) ones. Pretty soon it turned out that none of these frameworks did what I wanted. Actually they suggested using no framework at all for the situations I was looking to "solve" with a framework.&lt;/P&gt;
&lt;P&gt;At this point I rediscovered an &lt;A class="" title="Mocks Aren't Stubs" href="http://martinfowler.com/articles/mocksArentStubs.html" target=_blank mce_href="http://martinfowler.com/articles/mocksArentStubs.html"&gt;old article by Martin Fowler&lt;/A&gt;&amp;nbsp;and it became crystal clear that the reason I didn't find what I wanted in the mocking frameworks was because I really wanted a stub framework (or rather a stub helper). Because what I wanted was a simple way to reuse the algorithm "&lt;EM&gt;return 17 the first 7 times called and then throw an exception after that, unless called with argument 4711 in which case you should always return 42&lt;/EM&gt;". A pretty simple method to write in your stub if you just subclass and override the object you want to mock/stub but a little bit cumbersome in the long run if you do this a lot.&lt;/P&gt;
&lt;P&gt;During a discussion on a company internal mailing list for TDD it became clear that people generally use the term &lt;EM&gt;mock object&lt;/EM&gt; for almost every kind of non-production object used during testing. And when another large group do make the distinction between mocks, stubs, fakes and dummies you quickly run into trouble since you are no longer discussing the same thing. So here is yet another recap on what the different kinds of objects are so I can contribute to the cause, making world a better place to be. At least when talking about mocks &amp;amp; friends.&lt;/P&gt;
&lt;P&gt;
&lt;TABLE class="" border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class=""&gt;Object&lt;/TH&gt;
&lt;TH class=""&gt;Description&lt;/TH&gt;
&lt;TH class=""&gt;Comment&lt;/TH&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Dummy&lt;/TD&gt;
&lt;TD class=""&gt;Passed around and should never be used.&lt;/TD&gt;
&lt;TD class=""&gt;Should fail the tests if ever used.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Fake&lt;/TD&gt;
&lt;TD class=""&gt;Working implementation but takes short-cuts making them unsuitable for production.&lt;/TD&gt;
&lt;TD class=""&gt;For example an in memory database.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Stub&lt;/TD&gt;
&lt;TD class=""&gt;Quickly responds with a small number of known values.&lt;/TD&gt;
&lt;TD class=""&gt;Used during testing to mimic a certain behavior.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Mock&lt;/TD&gt;
&lt;TD class=""&gt;I like to think of mocks as stubs with the knowledge of usage added.&lt;/TD&gt;
&lt;TD class=""&gt;Used to verify that the user of the (mock)object makes the correct number of&amp;nbsp;calls&amp;nbsp;in the correct order.&lt;BR&gt;Many mock frameworks are designed to make verification of number of calls and/or order optional. But if you do not verify order or number of calls you're basically using a stub (but it is generated from a mock framework). &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8416129" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category></item><item><title>MFC is not dead</title><link>http://blogs.msdn.com/cellfish/archive/2008/04/12/mfc-is-not-dead.aspx</link><pubDate>Sat, 12 Apr 2008 10:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8384347</guid><dc:creator>cellfish</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8384347.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8384347</wfw:commentRss><description>&lt;P&gt;When I wrote my master's thesis was the first time I came in to contact with VC++ and MFC. I worked with MFC and VC++ quite a lot for a number of years but the last four or five years have not had much MFC work in it. When the .Net framework&amp;nbsp;came along with the possibility to write managed C++ applications I thought MFC was dead and didn't give it much more thought.&lt;/P&gt;
&lt;P&gt;Then I read about the new VC++2008 feature pack &lt;A class="" title="Johan Lindfors" href="http://blogs.msdn.com/johanl/archive/2008/04/07/visual-c-2008-feature-pack.aspx" mce_href="http://blogs.msdn.com/johanl/archive/2008/04/07/visual-c-2008-feature-pack.aspx"&gt;here&lt;/A&gt; and watched the &lt;A class="" title="Pat Brenner: New Updates to MFC in Visual Studio 2008" href="http://channel9.msdn.com/showpost.aspx?postid=355087" target=_blank mce_href="http://channel9.msdn.com/showpost.aspx?postid=355087"&gt;channel 9 video&lt;/A&gt; referenced. I noticed how the reporter also questions the MFC developer about the presumed death of MFC. Apparently&amp;nbsp;MFC is not dead as many, including me, thought. The reason I thought so was that I just assumed that with managed C++ no one would bother writing them unmanaged in MFC any more. But when I think a little more about it I guess there are lots of people still out there who are familiar with the MFC framework and who do not have the time and/or interest to learn the new managed equivalents. &lt;/P&gt;
&lt;P&gt;Some might say that you want to write MFC applications in stead of managed ones because the users do not have the .Net framework installed. A fair point but frankly I do not think that the user that will benefit from this MFC update (which for example includes office ribbon support&amp;nbsp;for office 2007) also already have the .Net framework installed.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Update: &lt;/STRONG&gt;&lt;/EM&gt;I noticed that this post was referenced by this one: &lt;A href="http://blog.stevienova.com/2008/04/12/why-is-mfc-not-dead/"&gt;http://blog.stevienova.com/2008/04/12/why-is-mfc-not-dead/&lt;/A&gt;. I realized I was not 100% clear in my post.&amp;nbsp;I must say that I agree with the author of that post - MFC will be used for many years for the reasons mentioned: People want to make applications that work across many different versions of Windows without installing the .Net framework. There was never a doubt in my mind that was the case. However I was surprised there was so much new development put into new versions of MFC since I guess all the new stuff will not work very well anyway. And much of the new stuff in MFC is there to mimic stuff seen in the managed framework and/or in latest versions of office/windows. And if you run Vista or Server 2008 I guess you will have the .net framework already installed and hence do not need to use MFC.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8384347" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/MFC/default.aspx">MFC</category></item></channel></rss>