2012年10月18日
摘要: 程序员的自我修养里有这么一段程序,作者认为这里的双重if可以使lock的调用开销最小。volatile T* p=0;T*GetInstance(){if(p==NULL){ lock(); if(p==NULL) p=new T; unlock();}return p;}第一个if显然是必要的,但对于第二个if,到底有没有必要,第一反应确实是有些奇怪。经过google发现原因如下:假设有两个线程A和B,当A调用GetInstance执行到第一个if时,P为空,在执行lock之前,A有可能被CPU临时挂起,假如没有第二个if,则进程B运行到同样位置之后,会lock并且创建对象,然后unl... 阅读全文
posted @ 2012-10-18 20:23 Zaiping 阅读(494) 评论(0) 推荐(0) 编辑
摘要: inline function内联函数macro宏You should use an inline function when you wish to optimize function calls. You may consider an inline function as a macro that copies the function contents (inline) to code segment where the function is called. However, inline function is very different from a macro (google 阅读全文
posted @ 2012-10-18 16:40 Zaiping 阅读(275) 评论(0) 推荐(0) 编辑