<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3767458728009089685</id><updated>2012-02-10T19:36:55.086-08:00</updated><category term='linux'/><category term='nothrow'/><category term='pool'/><category term='explicit'/><category term='implicit'/><category term='cygwin'/><category term='keyword'/><category term='unix'/><category term='threadpool'/><category term='bad_alloc'/><category term='exception'/><category term='conversion'/><category term='new'/><category term='constructors'/><category term='c++'/><category term='thread'/><title type='text'>GlueMesh</title><subtitle type='html'>This is our endeavor to share with rest of the world what we know about software engineering and the business around it.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://shobhitgupta12.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://shobhitgupta12.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Shobhit</name><uri>http://www.blogger.com/profile/02518535335870643826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://bp1.blogger.com/_yOp72e2xLkk/R83TfkP8RBI/AAAAAAAABYE/22T3EggopVY/S220/shobhit_small_pic.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3767458728009089685.post-7530975202496665683</id><published>2009-02-07T10:32:00.000-08:00</published><updated>2009-02-07T13:47:05.143-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='conversion'/><category scheme='http://www.blogger.com/atom/ns#' term='constructors'/><category scheme='http://www.blogger.com/atom/ns#' term='keyword'/><category scheme='http://www.blogger.com/atom/ns#' term='explicit'/><category scheme='http://www.blogger.com/atom/ns#' term='implicit'/><title type='text'>Recovery from implicit habits by using explicit keyword</title><content type='html'>This might be something ordinary for many. &lt;br /&gt;But its only very recently that I have changed my coding style.&lt;br /&gt;I find myself using the keyword explicit almost always now. Thus I would like to share why I started. &lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;class Foo&lt;br /&gt;{&lt;br /&gt;  public:&lt;br /&gt;    Foo(int x) {}&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Its an innocent Foo class with a constructor that accepts an integer. &lt;br /&gt;&lt;br /&gt;Constructors with single parameters servers us dual purpose:&lt;br /&gt;1. just like all constructors it assists in construction of an object.&lt;br /&gt;2. it behaves as an implicit conversion operator, that converts from type int to type Foo.&lt;br /&gt;&lt;br /&gt;Consider this:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;Foo f1(0);           // line 1 - works&lt;br /&gt;Foo f2 = 0L;         // line 2 - works&lt;br /&gt;Foo f3 = NULL;       // line 3 - works&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Line 1 is straight forward.&lt;br /&gt;Line 2 is quite intuitively doing an implicit conversion from long to int.&lt;br /&gt;Line 3 is plain WRONG. Its supposed to be a pointer type, but let say a typo occured. Surprisingly enough, it does compile.&lt;br /&gt;&lt;br /&gt;Line 2 &amp; 3 works because compiler internally turns off its static type checking (due to implicit conversion).&lt;br /&gt;&lt;br /&gt;We would definitely want to avoid unintended usages like the "line 3" example.&lt;br /&gt;&lt;br /&gt;This is something that we can achieve by the "explicit" keyword.&lt;br /&gt;&lt;br /&gt;The only place explicit keyword can be used is in constructors.&lt;br /&gt;&lt;br /&gt;For eg:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;class Foo&lt;br /&gt;{&lt;br /&gt;  public:&lt;br /&gt;   explicit Foo(int x) {}&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now if we have explicit, these are the new results:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;Foo f1(0);           // line 1 - works&lt;br /&gt;Foo f2 = 0L;         // line 2 - doens't work&lt;br /&gt;Foo f3 = NULL;       // line 3 - doesn't work&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and in order make those lines work we will have to be more explicit during construction.&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;Foo f1(0);                       // line 1 - works&lt;br /&gt;Foo f2 = static_cast&amp;lt;Foo&amp;gt; (0L);  // line 2 - works&lt;br /&gt;Foo *f3 = NULL;                  // line 3 - works&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Explicit keyword helps us not to do silly mistakes which can turn be quite lethal if not paid attention to.&lt;br /&gt;&lt;br /&gt;IMHO, its wise to always use the explicit keyword in your constructors with single parameter.&lt;br /&gt;It also makes sense to use explicit keyword with constructors that have more than x parameters but (x-1) parameters have default values.&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_yOp72e2xLkk/SY3-LFaVggI/AAAAAAAAFCQ/-DynKR1tozU/s1600-h/shobhit-signature.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 82px; height: 48px;" src="http://1.bp.blogspot.com/_yOp72e2xLkk/SY3-LFaVggI/AAAAAAAAFCQ/-DynKR1tozU/s320/shobhit-signature.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5300171802869596674" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3767458728009089685-7530975202496665683?l=shobhitgupta12.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shobhitgupta12.blogspot.com/feeds/7530975202496665683/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3767458728009089685&amp;postID=7530975202496665683' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default/7530975202496665683'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default/7530975202496665683'/><link rel='alternate' type='text/html' href='http://shobhitgupta12.blogspot.com/2009/02/recovery-from-implicit-habits-by-using.html' title='Recovery from implicit habits by using explicit keyword'/><author><name>Shobhit</name><uri>http://www.blogger.com/profile/02518535335870643826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://bp1.blogger.com/_yOp72e2xLkk/R83TfkP8RBI/AAAAAAAABYE/22T3EggopVY/S220/shobhit_small_pic.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_yOp72e2xLkk/SY3-LFaVggI/AAAAAAAAFCQ/-DynKR1tozU/s72-c/shobhit-signature.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3767458728009089685.post-8998004639978904464</id><published>2008-03-05T19:53:00.000-08:00</published><updated>2008-03-08T14:22:05.118-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='bad_alloc'/><category scheme='http://www.blogger.com/atom/ns#' term='new'/><category scheme='http://www.blogger.com/atom/ns#' term='exception'/><category scheme='http://www.blogger.com/atom/ns#' term='nothrow'/><title type='text'>new never returns NULL</title><content type='html'>Recently I gave the Brainbench c++ test and came across an interesting question.&lt;br /&gt;&lt;br /&gt;It was something like:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;{&lt;br /&gt;   int *i = new int[10];&lt;br /&gt;   if(!i)&lt;br /&gt;   {&lt;br /&gt;      NoMemoryHandler();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In this question, the right answer is "NoMemoryHandler() is never called".&lt;br /&gt;&lt;br /&gt;Thats because, in c++ new operator never returns a NULL. In case of failure new simply throws an exception std::bad_alloc.&lt;br /&gt;&lt;br /&gt;Now if you catch the exception and handle the situation, then its fine. Otherwise, if you don't catch the exception, your lovely program will get terminated. (Thats so uncool!)&lt;br /&gt;&lt;br /&gt;Now everytime whenever you gotta write code, it should be like:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;{&lt;br /&gt;   try&lt;br /&gt;   {&lt;br /&gt;      int *i = new int[10];&lt;br /&gt;   }&lt;br /&gt;   catch(bad_alloc &amp;amp;e)&lt;br /&gt;   {&lt;br /&gt;      cout &lt;&lt; "OK, new failed!! But on the positive side, I am aware of it !!" &lt;&lt; endl;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Fortunately to our rescue there is another version of new which doesn't throw anything on failing.&lt;br /&gt;&lt;br /&gt;With reference to &lt;a href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00589.html"&gt;GCC's online docs&lt;/a&gt; we come to know about the nothrow version of new.&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;{&lt;br /&gt;   int *i = new (nothrow) int[10];&lt;br /&gt;   if(!i)&lt;br /&gt;   {&lt;br /&gt;      cout &lt;&lt; "new failed!!  but now I am not a slave of exceptions !!" &lt;&lt; endl;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;When I use the standard version of new, I have to handle the exception it throws on failure, else my program gets terminated.&lt;br /&gt;If I use the "nothrow" version of new, I still need to write some healing code, but then I am free of unnecessary try{} and catch(){} blocks.&lt;br /&gt;&lt;br /&gt;Until and unless I have to customize "new" for specific needs, I would always prefer the "nothrow" verison.&lt;br /&gt;&lt;br /&gt;-----------------------------------------------------------------------&lt;br /&gt;And yeah...  I got a 4.69 score. Transcript id: &lt;a href="http://www.brainbench.com/transcript.jsp?pid=7416277"&gt;7416277&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_yOp72e2xLkk/R9FSuctoCBI/AAAAAAAABZM/4eI49YGFUFU/s1600-h/shobhit-signature.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://bp0.blogger.com/_yOp72e2xLkk/R9FSuctoCBI/AAAAAAAABZM/4eI49YGFUFU/s320/shobhit-signature.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5175008404760430610" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3767458728009089685-8998004639978904464?l=shobhitgupta12.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shobhitgupta12.blogspot.com/feeds/8998004639978904464/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3767458728009089685&amp;postID=8998004639978904464' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default/8998004639978904464'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default/8998004639978904464'/><link rel='alternate' type='text/html' href='http://shobhitgupta12.blogspot.com/2008/03/new-never-returns-null.html' title='new never returns NULL'/><author><name>Shobhit</name><uri>http://www.blogger.com/profile/02518535335870643826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://bp1.blogger.com/_yOp72e2xLkk/R83TfkP8RBI/AAAAAAAABYE/22T3EggopVY/S220/shobhit_small_pic.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_yOp72e2xLkk/R9FSuctoCBI/AAAAAAAABZM/4eI49YGFUFU/s72-c/shobhit-signature.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3767458728009089685.post-4436441880078340839</id><published>2008-03-02T19:27:00.000-08:00</published><updated>2008-03-07T06:36:14.188-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='pool'/><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='threadpool'/><category scheme='http://www.blogger.com/atom/ns#' term='thread'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='unix'/><category scheme='http://www.blogger.com/atom/ns#' term='cygwin'/><title type='text'>Thread Pool implementation using c++</title><content type='html'>&lt;span style="font-size:78%;"&gt;&lt;/span&gt;I was browsing through some Design Patterns over the internet, and I recently came across the &lt;a href="http://en.wikipedia.org/wiki/Thread_pool_pattern"&gt;Thread Pool pattern&lt;/a&gt;. My natural reaction was to google around for some implementations.&lt;br /&gt;&lt;br /&gt;I liked  Blake Thompson's win32 implementation on &lt;a href="http://softwarecommunity.intel.com/articles/eng/2514.htm"&gt;Intel's website&lt;/a&gt;. Inspired from it, I thought of writing an implementation for unix/linux platform.&lt;br /&gt;&lt;br /&gt;ThreadPool class manages all the ThreadPool related                 activities. This includes keeping track of idle threads  and synchronizations between all threads.&lt;br /&gt;&lt;br /&gt;ThreadPool class uses a WorkerThread class.&lt;br /&gt;&lt;br /&gt;Now this WorkerThread is having a virtual function executeThis().&lt;br /&gt;The user needs to subclass WorkerThread and implement executeThis() to assign the tasks.&lt;br /&gt;&lt;br /&gt;You can download the code and documentation from &lt;a href="http://code.google.com/p/cppthreadpool/"&gt;http://code.google.com/p/cppthreadpool/&lt;/a&gt;&lt;br /&gt;This code is licensed under GPL v3. So feel free to use it, modify it, and whatever it.&lt;br /&gt;&lt;br /&gt;I have currently tested it under cygwin environment, but I am expecting it to work in unix and linux environments as well (since it uses POSIX threads). It will be great if someone with unix/linux environment can test it.&lt;br /&gt;&lt;br /&gt;If you have any doubts about how to use this Thread Pool implementation, then please feel free to ask me at shobhitgupta12@gmail.com&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_yOp72e2xLkk/R9FSuctoCBI/AAAAAAAABZM/4eI49YGFUFU/s1600-h/shobhit-signature.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://bp0.blogger.com/_yOp72e2xLkk/R9FSuctoCBI/AAAAAAAABZM/4eI49YGFUFU/s320/shobhit-signature.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5175008404760430610" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3767458728009089685-4436441880078340839?l=shobhitgupta12.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://code.google.com/p/cppthreadpool/' title='Thread Pool implementation using c++'/><link rel='enclosure' type='' href='http://code.google.com/p/cppthreadpool/' length='0'/><link rel='replies' type='application/atom+xml' href='http://shobhitgupta12.blogspot.com/feeds/4436441880078340839/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3767458728009089685&amp;postID=4436441880078340839' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default/4436441880078340839'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3767458728009089685/posts/default/4436441880078340839'/><link rel='alternate' type='text/html' href='http://shobhitgupta12.blogspot.com/2008/03/thread-pool-implementation-using-c.html' title='Thread Pool implementation using c++'/><author><name>Shobhit</name><uri>http://www.blogger.com/profile/02518535335870643826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://bp1.blogger.com/_yOp72e2xLkk/R83TfkP8RBI/AAAAAAAABYE/22T3EggopVY/S220/shobhit_small_pic.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_yOp72e2xLkk/R9FSuctoCBI/AAAAAAAABZM/4eI49YGFUFU/s72-c/shobhit-signature.png' height='72' width='72'/><thr:total>6</thr:total></entry></feed>
