Strong Guarantee using Transaction
Posted on April 29th, 2008 at 23:24 by fr3@K

Abrahams Guarantees

在 C++ 的世界裡, 正確的 exception handling 是專業的 C++ programmer 不可或缺的技巧. 雖然它的概念並不困難, 但實作起來卻常不見得那麼容易.

要做到正確的 exception handling, 首先必須要了解什麼是 exception safety. 一個需要與 exception 打交道的 component 可在其介面實作人稱 Abrahams guarantees 的三種 exception safety 保證之一:

  1. The basic guarantee
  2. 允許操作失敗時改變物件的狀態, 但不能有 resource leak. 且該物件的狀態必須是可靠的仍然可以被解構, 操作失敗後該物件的狀態可以是不完全能被預測的.

  3. The strong guarantee
  4. 操作後的狀態只能是成功完成, 或是將該物件回復到操作之前的狀態並拋出一個 exception.

  5. The no-throw guarantee
  6. 操作不會拋出 exception.

(more…)

del.icio.us:Strong Guarantee using Transaction digg:Strong Guarantee using Transaction spurl:Strong Guarantee using Transaction newsvine:Strong Guarantee using Transaction furl:Strong Guarantee using Transaction Y!:Strong Guarantee using Transaction 黑米共享書籤:Strong Guarantee using Transaction 推推王:Strong Guarantee using Transaction
re: Socket Programming in C 常犯的錯誤
Posted on April 22nd, 2008 at 12:25 by fr3@K

昨天在網上閒逛, 在樂多上看到篇名為 “Socket Programming in C 常犯的錯誤” 的短文. 這短文作者的動機很好, 指出常見到的錯誤並提供個人看法與可能較好的作法. 這樣的行為很值得鼓勵. 但極可惜的是, 結果卻是很不理想的. 容我說句重話, 如果是考試的話該文是得不到我的分數的.
(more…)

del.icio.us:re: Socket Programming in C 常犯的錯誤 digg:re: Socket Programming in C 常犯的錯誤 spurl:re: Socket Programming in C 常犯的錯誤 newsvine:re: Socket Programming in C 常犯的錯誤 furl:re: Socket Programming in C 常犯的錯誤 Y!:re: Socket Programming in C 常犯的錯誤 黑米共享書籤:re: Socket Programming in C 常犯的錯誤 推推王:re: Socket Programming in C 常犯的錯誤
The Free Launch is Back
Posted on April 15th, 2008 at 16:08 by fr3@K

From the main page of MCSTL:

The Multi-Core Standard Template Library (MCSTL) is a parallel implementation of the standard C++ library. It makes use of multiple processors and/or multiple cores of a processor with shared memory. It blends in transparently and there is in principle no change necessary in the program itself.

(more…)

del.icio.us:The Free Launch is Back digg:The Free Launch is Back spurl:The Free Launch is Back newsvine:The Free Launch is Back furl:The Free Launch is Back Y!:The Free Launch is Back 黑米共享書籤:The Free Launch is Back 推推王:The Free Launch is Back
不是李白也能 C++
Posted on April 13th, 2008 at 3:16 by fr3@K

除了語法上的困難, 另一個 C++ 常被人嫌的特性就是不如 C#(.Net)/Java/Python 之流般擁有大量標準的 library 或針對特定功能的準標準 library. 寫上一篇 (五種寫 For Loop 的方法) 時, 突然體會到這樣的現象似乎是源自 C++ 的設計與演化, 很可能是難以避免的結果.
(more…)

del.icio.us:不是李白也能 C++ digg:不是李白也能 C++ spurl:不是李白也能 C++ newsvine:不是李白也能 C++ furl:不是李白也能 C++ Y!:不是李白也能 C++ 黑米共享書籤:不是李白也能 C++ 推推王:不是李白也能 C++
五種寫 For Loop 的方法
Posted on April 11th, 2008 at 6:23 by fr3@K

同樣是寫一個迭代的 loop, 至少有下面兩種標準 (C++98), 兩種半標準 (Boost), 以及一種未來式 (C++0x) 的寫法:
(more…)

del.icio.us:五種寫 For Loop 的方法 digg:五種寫 For Loop 的方法 spurl:五種寫 For Loop 的方法 newsvine:五種寫 For Loop 的方法 furl:五種寫 For Loop 的方法 Y!:五種寫 For Loop 的方法 黑米共享書籤:五種寫 For Loop 的方法 推推王:五種寫 For Loop 的方法
Source Code [Benchmarking Sequential Iterators]
Posted on March 17th, 2008 at 23:39 by fr3@K

Source code used to produce results in my previous post is available for download here.

Please note that Boost.Date_Time is required in order to build it.

del.icio.us:Source Code [Benchmarking Sequential Iterators] digg:Source Code [Benchmarking Sequential Iterators] spurl:Source Code [Benchmarking Sequential Iterators] newsvine:Source Code [Benchmarking Sequential Iterators] furl:Source Code [Benchmarking Sequential Iterators] Y!:Source Code [Benchmarking Sequential Iterators] 黑米共享書籤:Source Code [Benchmarking Sequential Iterators] 推推王:Source Code [Benchmarking Sequential Iterators]
Benchmarking Sequential Iterators
Posted on March 16th, 2008 at 12:20 by fr3@K

這幾天有空, 終於把一項幾個月前就開始做的 benchmark 完成了.


看過好幾次有同事的 code, 明明就是使用 vector 來管理一串 element, 卻硬要把被管理的 element 的 pointer 取出, 以代替 vector 本身的 iterator 來做迭代. 有些同事說是不熟悉 iterator 的特性與操作方式. 這個理由很難讓人接受. 身為職業 C++ programmer, 標準的 container 與 iterator 是無論如何也要掌握住的技巧. 促成我搞這個 benchmark 的原因是有另外的同事跟我說, 取 pointer 出來迭代是因為 vector 的 iterator 比 pointer 慢很多, 並且是指數級別的差距!

在進行這項 benchmark 之前, 說以 vector 的 iterator 迭代比 pointer 慢, (起碼不會快過 pointer) 我絕對不會意外. 我猜大多數人也預期會看這樣的結果. 即便如此, 我還是很難想像其差異會大到如一位同事告訴我的. 因此我寫了個程式來 benchmark. 將 pointer 以及各種 STL 的 sequence container 的 iterator 做同樣的迭代, 把所使用的時間紀錄下來, 製作成表格以比較其間差異. 讓數字來說話.
(more…)

del.icio.us:Benchmarking Sequential Iterators digg:Benchmarking Sequential Iterators spurl:Benchmarking Sequential Iterators newsvine:Benchmarking Sequential Iterators furl:Benchmarking Sequential Iterators Y!:Benchmarking Sequential Iterators 黑米共享書籤:Benchmarking Sequential Iterators 推推王:Benchmarking Sequential Iterators
拒絕 Singleton
Posted on December 12th, 2007 at 1:32 by fr3@K

去年我寫過一篇文章, 大意是在說 大多數人所使用的 Singleton 實作都是有問題的. 在文章靠近後面的部份, 我也介紹了一種較少為人知, 我常用在 lifetime 是全局但 scope 是局部的 (global) 變數的 Singleton 實作. 雖然這實作離 fool proof 還差得遠, 在使用者能夠正確地 include header 的情況下, 它可能已經是最接近 bullet proof 的實作了.

“好吧, 那我就用類似你的 Singleton 實作總行了吧!?” 先別急著動手改 code, 實作是容易更換的, 但使用 Singleton Pattern 的原因卻與更重要的介面, 甚至架構的設計思維息息相關. 工作的實務經驗以及本身的興趣研究讓我體會到, 大多數的情況下 Singleton 的使用是可以避免的. 在這些非必要情況卻依然採用 Singleton 的原因常是:

  • 沒把問題想清楚
  • 好玩
  • 因為有個叫作 Singleton 的 pattern

(more…)

del.icio.us:拒絕 Singleton digg:拒絕 Singleton spurl:拒絕 Singleton newsvine:拒絕 Singleton furl:拒絕 Singleton Y!:拒絕 Singleton 黑米共享書籤:拒絕 Singleton 推推王:拒絕 Singleton
WIN32 的 _TCHAR 與 std::wstring 的問題
Posted on November 20th, 2007 at 3:28 by fr3@K

我知道很多人一直是這樣做的, 可是我從沒搞懂為什麼在 windows 平台上, 用上了 _TCHAR, _TEXT 再 define 個 _UNICODE. 或是用上了 whar_t 與 std::wstring. 就能算是 Unicode 化的程式?
(more…)

del.icio.us:WIN32 的 _TCHAR 與 std::wstring 的問題 digg:WIN32 的 _TCHAR 與 std::wstring 的問題 spurl:WIN32 的 _TCHAR 與 std::wstring 的問題 newsvine:WIN32 的 _TCHAR 與 std::wstring 的問題 furl:WIN32 的 _TCHAR 與 std::wstring 的問題 Y!:WIN32 的 _TCHAR 與 std::wstring 的問題 黑米共享書籤:WIN32 的 _TCHAR 與 std::wstring 的問題 推推王:WIN32 的 _TCHAR 與 std::wstring 的問題
Deque as Default Container
Posted on November 12th, 2007 at 22:23 by fr3@K

搞不清楚 list, vectordeque 的差異?

最好的方法當然是能深入了解 C++ Standard 對它們的規範. 否則可以參考下面幾個 check point:

  • Container 所包含的 (element) sequence 在 memory 的 layout 必須是連續的. 唯一的選擇是 vector
  • 需要 iterator 的位置在插入一個新的 element 或 remove 另一個 element 之後保持有效. 只有 list 能滿足這需求
  • 常在 sequence 中間 (非頭尾) 位置 insert 或 remove. list 會是較好的選擇
  • vectordeque 都可以滿足以隨機方式存取 sequence 中的 element. 唯前者效能較好 (constant time)
  • 在還沒確認需求時, 建議以 deque 做為 default sequence container template

延伸閱讀 GotW #54: Using Vector and Deque.

del.icio.us:Deque as Default Container digg:Deque as Default Container spurl:Deque as Default Container newsvine:Deque as Default Container furl:Deque as Default Container Y!:Deque as Default Container 黑米共享書籤:Deque as Default Container 推推王:Deque as Default Container

Page 1 of 4[ 1 ]234»