鍍金池/ 教程/ C/ D.6 <ratio>頭文件
3.4 本章總結
6.3 基于鎖設計更加復雜的數據結構
6.1 為并發(fā)設計的意義何在?
5.2 <code>C++</code>中的原子操作和原子類型
A.7 自動推導變量類型
2.1 線程管理的基礎
8.5 在實踐中設計并發(fā)代碼
2.4 運行時決定線程數量
2.2 向線程函數傳遞參數
第4章 同步并發(fā)操作
2.3 轉移線程所有權
8.3 為多線程性能設計數據結構
6.4 本章總結
7.3 對于設計無鎖數據結構的指導建議
關于這本書
A.1 右值引用
2.6 本章總結
D.2 &lt;condition_variable&gt;頭文件
A.6 變參模板
6.2 基于鎖的并發(fā)數據結構
4.5 本章總結
A.9 本章總結
前言
第10章 多線程程序的測試和調試
5.4 本章總結
第9章 高級線程管理
5.1 內存模型基礎
2.5 識別線程
第1章 你好,C++的并發(fā)世界!
1.2 為什么使用并發(fā)?
A.5 Lambda函數
第2章 線程管理
4.3 限定等待時間
D.3 &lt;atomic&gt;頭文件
10.2 定位并發(fā)錯誤的技術
附錄B 并發(fā)庫的簡單比較
5.3 同步操作和強制排序
A.8 線程本地變量
第8章 并發(fā)代碼設計
3.3 保護共享數據的替代設施
附錄D C++線程庫參考
第7章 無鎖并發(fā)數據結構設計
D.7 &lt;thread&gt;頭文件
D.1 &lt;chrono&gt;頭文件
4.1 等待一個事件或其他條件
A.3 默認函數
附錄A 對<code>C++</code>11語言特性的簡要介紹
第6章 基于鎖的并發(fā)數據結構設計
封面圖片介紹
7.2 無鎖數據結構的例子
8.6 本章總結
8.1 線程間劃分工作的技術
4.2 使用期望等待一次性事件
8.4 設計并發(fā)代碼的注意事項
D.5 &lt;mutex&gt;頭文件
3.1 共享數據帶來的問題
資源
9.3 本章總結
10.3 本章總結
10.1 與并發(fā)相關的錯誤類型
D.4 &lt;future&gt;頭文件
3.2 使用互斥量保護共享數據
9.1 線程池
1.1 何謂并發(fā)
9.2 中斷線程
4.4 使用同步操作簡化代碼
A.2 刪除函數
1.3 C++中的并發(fā)和多線程
1.4 開始入門
第5章 C++內存模型和原子類型操作
消息傳遞框架與完整的ATM示例
8.2 影響并發(fā)代碼性能的因素
7.1 定義和意義
D.6 &lt;ratio&gt;頭文件
A.4 常量表達式函數
7.4 本章總結
1.5 本章總結
第3章 線程間共享數據

D.6 &lt;ratio&gt;頭文件

<ratio>頭文件提供在編譯時進行的計算。

頭文件內容

namespace std
{
  template<intmax_t N,intmax_t D=1>
  class ratio;

  // ratio arithmetic
  template <class R1, class R2>
  using ratio_add = see description;

  template <class R1, class R2>
  using ratio_subtract = see description;

  template <class R1, class R2>
  using ratio_multiply = see description;

  template <class R1, class R2>
  using ratio_divide = see description;

  // ratio comparison
  template <class R1, class R2>
  struct ratio_equal;

  template <class R1, class R2>
  struct ratio_not_equal;

  template <class R1, class R2>
  struct ratio_less;

  template <class R1, class R2>
  struct ratio_less_equal;

  template <class R1, class R2>
  struct ratio_greater;

  template <class R1, class R2>
  struct ratio_greater_equal;

  typedef ratio<1, 1000000000000000000> atto;
  typedef ratio<1, 1000000000000000> femto;
  typedef ratio<1, 1000000000000> pico;
  typedef ratio<1, 1000000000> nano;
  typedef ratio<1, 1000000> micro;
  typedef ratio<1, 1000> milli;
  typedef ratio<1, 100> centi;
  typedef ratio<1, 10> deci;
  typedef ratio<10, 1> deca;
  typedef ratio<100, 1> hecto;
  typedef ratio<1000, 1> kilo;
  typedef ratio<1000000, 1> mega;
  typedef ratio<1000000000, 1> giga;
  typedef ratio<1000000000000, 1> tera;
  typedef ratio<1000000000000000, 1> peta;
  typedef ratio<1000000000000000000, 1> exa;
}

D.6.1 std::ratio類型模板

std::ratio類型模板提供了一種對在編譯時進行計算的機制,通過調用合理的數,例如:半(std::ratio<1,2>),2/3(std::ratio<2, 3>)或15/43(std::ratio<15, 43>)。其使用在C++標準庫內部,用于初始化std::chrono::duration類型模板。

類型定義

template <intmax_t N, intmax_t D = 1>
class ratio
{
public:
  typedef ratio<num, den> type;
  static constexpr intmax_t num= see below;
  static constexpr intmax_t den= see below;
};

要求
D不能為0。

描述
num和den分別為分子和分母,構造分數N/D。den總是正數。當N和D的符號相同,那么num為正數;否則num為負數。

例子

ratio<4,6>::num == 2
ratio<4,6>::den == 3
ratio<4,-6>::num == -2
ratio<4,-6>::den == 3

D.6.2 std::ratio_add模板別名

std::ratio_add模板別名提供了兩個std::ratio在編譯時相加的機制(使用有理計算)。

定義

template <class R1, class R2>
using ratio_add = std::ratio<see below>;

先決條件
R1和R2必須使用std::ratio進行初始化。

效果
ratio_add<R1, R2>被定義為一個別名,如果兩數可以計算,且無溢出,該類型可以表示兩個std::ratio對象R1和R2的和。如果計算出來的結果溢出了,那么程序里面就有問題了。在算術溢出的情況下,std::ratio_add<R1, R2>應該應該與std::ratio<R1::num * R2::den + R2::num * R1::den, R1::den * R2::den>相同。

例子

std::ratio_add<std::ratio<1,3>, std::ratio<2,5> >::num == 11
std::ratio_add<std::ratio<1,3>, std::ratio<2,5> >::den == 15

std::ratio_add<std::ratio<1,3>, std::ratio<7,6> >::num == 3
std::ratio_add<std::ratio<1,3>, std::ratio<7,6> >::den == 2

D.6.3 std::ratio_subtract模板別名

std::ratio_subtract模板別名提供兩個std::ratio數在編譯時進行相減(使用有理計算)。

定義

template <class R1, class R2>
using ratio_subtract = std::ratio<see below>;

先決條件
R1和R2必須使用std::ratio進行初始化。

效果
ratio_add<R1, R2>被定義為一個別名,如果兩數可以計算,且無溢出,該類型可以表示兩個std::ratio對象R1和R2的和。如果計算出來的結果溢出了,那么程序里面就有問題了。在算術溢出的情況下,std::ratio_subtract<R1, R2>應該應該與std::ratio<R1::num * R2::den - R2::num * R1::den, R1::den * R2::den>相同。

例子

std::ratio_subtract<std::ratio<1,3>, std::ratio<1,5> >::num == 2
std::ratio_subtract<std::ratio<1,3>, std::ratio<1,5> >::den == 15

std::ratio_subtract<std::ratio<1,3>, std::ratio<7,6> >::num == -5
std::ratio_subtract<std::ratio<1,3>, std::ratio<7,6> >::den == 6

D.6.4 std::ratio_multiply模板別名

std::ratio_multiply模板別名提供兩個std::ratio數在編譯時進行相乘(使用有理計算)。

定義

template <class R1, class R2>
using ratio_multiply = std::ratio<see below>;

先決條件
R1和R2必須使用std::ratio進行初始化。

效果
ratio_add<R1, R2>被定義為一個別名,如果兩數可以計算,且無溢出,該類型可以表示兩個std::ratio對象R1和R2的和。如果計算出來的結果溢出了,那么程序里面就有問題了。在算術溢出的情況下,std::ratio_multiply<R1, R2>應該應該與std::ratio<R1::num * R2::num, R1::den * R2::den>相同。

例子

std::ratio_multiply<std::ratio<1,3>, std::ratio<2,5> >::num == 2
std::ratio_multiply<std::ratio<1,3>, std::ratio<2,5> >::den == 15

std::ratio_multiply<std::ratio<1,3>, std::ratio<15,7> >::num == 5
std::ratio_multiply<std::ratio<1,3>, std::ratio<15,7> >::den == 7

D.6.5 std::ratio_divide模板別名

std::ratio_divide模板別名提供兩個std::ratio數在編譯時進行相除(使用有理計算)。

定義

template <class R1, class R2>
using ratio_multiply = std::ratio<see below>;

先決條件
R1和R2必須使用std::ratio進行初始化。

效果
ratio_add<R1, R2>被定義為一個別名,如果兩數可以計算,且無溢出,該類型可以表示兩個std::ratio對象R1和R2的和。如果計算出來的結果溢出了,那么程序里面就有問題了。在算術溢出的情況下,std::ratio_multiply<R1, R2>應該應該與std::ratio<R1::num * R2::num * R2::den, R1::den * R2::den>相同。

例子

std::ratio_divide<std::ratio<1,3>, std::ratio<2,5> >::num == 5
std::ratio_divide<std::ratio<1,3>, std::ratio<2,5> >::den == 6

std::ratio_divide<std::ratio<1,3>, std::ratio<15,7> >::num == 7
std::ratio_divide<std::ratio<1,3>, std::ratio<15,7> >::den == 45

D.6.6 std::ratio_equal類型模板

std::ratio_equal類型模板提供在編譯時比較兩個std::ratio數(使用有理計算)。

類型定義

template <class R1, class R2>
class ratio_equal:
  public std::integral_constant<
    bool,(R1::num == R2::num) && (R1::den == R2::den)>
{};

先決條件
R1和R2必須使用std::ratio進行初始化。

例子

std::ratio_equal<std::ratio<1,3>, std::ratio<2,6> >::value == true
std::ratio_equal<std::ratio<1,3>, std::ratio<1,6> >::value == false
std::ratio_equal<std::ratio<1,3>, std::ratio<2,3> >::value == false
std::ratio_equal<std::ratio<1,3>, std::ratio<1,3> >::value == true

D.6.7 std::ratio_not_equal類型模板

std::ratio_not_equal類型模板提供在編譯時比較兩個std::ratio數(使用有理計算)。

類型定義

template <class R1, class R2>
class ratio_not_equal:
  public std::integral_constant<bool,!ratio_equal<R1,R2>::value>
{};

先決條件
R1和R2必須使用std::ratio進行初始化。

例子

std::ratio_not_equal<std::ratio<1,3>, std::ratio<2,6> >::value == false
std::ratio_not_equal<std::ratio<1,3>, std::ratio<1,6> >::value == true
std::ratio_not_equal<std::ratio<1,3>, std::ratio<2,3> >::value == true
std::ratio_not_equal<std::ratio<1,3>, std::ratio<1,3> >::value == false

D.6.8 std::ratio_less類型模板

std::ratio_less類型模板提供在編譯時比較兩個std::ratio數(使用有理計算)。

類型定義

template <class R1, class R2>
class ratio_less:
  public std::integral_constant<bool,see below>
{};

先決條件
R1和R2必須使用std::ratio進行初始化。

效果
std::ratio_less<R1,R2>可通過std::integral_constant<bool, value >導出,這里value為(R1::num*R2::den) < (R2::num*R1::den)。如果有可能,需要實現(xiàn)使用一種機制來避免計算結果已出。當溢出發(fā)生,那么程序中就肯定有錯誤。

例子

std::ratio_less<std::ratio<1,3>, std::ratio<2,6> >::value == false
std::ratio_less<std::ratio<1,6>, std::ratio<1,3> >::value == true
std::ratio_less<
  std::ratio<999999999,1000000000>,
  std::ratio<1000000001,1000000000> >::value == true
std::ratio_less<
  std::ratio<1000000001,1000000000>,
  std::ratio<999999999,1000000000> >::value == false

D.6.9 std::ratio_greater類型模板

std::ratio_greater類型模板提供在編譯時比較兩個std::ratio數(使用有理計算)。

類型定義

template <class R1, class R2>
class ratio_greater:
  public std::integral_constant<bool,ratio_less<R2,R1>::value>
{};

先決條件
R1和R2必須使用std::ratio進行初始化。

D.6.10 std::ratio_less_equal類型模板

std::ratio_less_equal類型模板提供在編譯時比較兩個std::ratio數(使用有理計算)。

類型定義

template <class R1, class R2>
class ratio_less_equal:
  public std::integral_constant<bool,!ratio_less<R2,R1>::value>
{};

先決條件
R1和R2必須使用std::ratio進行初始化。

D.6.11 std::ratio_greater_equal類型模板

std::ratio_greater_equal類型模板提供在編譯時比較兩個std::ratio數(使用有理計算)。

類型定義

template <class R1, class R2>
class ratio_greater_equal:
  public std::integral_constant<bool,!ratio_less<R1,R2>::value>
{};

先決條件
R1和R2必須使用std::ratio進行初始化。