• C++11之this_thread namespace


    C++11中对于多线程并发编程提供了强大的支持,在this_thread名字空间中提供了几个常用函数get_id, sleep_for, sleep_until和yield。

    yield

    其中std::this_thread::yield对操作系统的调度器发出请求,主动让出执行权给其他的可运行线程。它的具体行为跟具体的实现相关,例如跟调度器算法相关。

    下面简单的代码示例中循环100万次,每到一万的整数倍时主动让出执行权。

    1. // demonstration of std::this_thread::yield
    2. // by Bruce Jia
    3. #include
    4. #include
    5. #include
    6. class HighResStopwatch
    7. {
    8. private:
    9. std::chrono::time_point start;
    10. public:
    11. HighResStopwatch() noexcept: start(std::chrono::high_resolution_clock::now()) {
    12. }
    13. ~HighResStopwatch() noexcept {
    14. auto elapsed = std::chrono::high_resolution_clock::now() - start;
    15. std::cout << std::chrono::duration_cast(elapsed).count()
    16. << " microseconds\n";
    17. }
    18. HighResStopwatch(HighResStopwatch &) = delete;
    19. void operator = (HighResStopwatch &) = delete;
    20. };
    21. int main()
    22. {
    23. long maxCount = 1000000L;
    24. int j = 1;
    25. for (long i=1; i<=maxCount; i++) {
    26. if (i % 10000 == 0) {
    27. std::cout << "yield " << j++ << std::endl;
    28. HighResStopwatch watch;
    29. std::this_thread::yield();
    30. }
    31. }
    32. }

    这里一个简单的类HighResStopwatch在析构的时候会打印出它的存活时间,近似等于出让执行权的时间。输出类似如下

    1. yield 1
    2. 1 microseconds
    3. yield 2
    4. 260 microseconds
    5. yield 3
    6. 0 microseconds
    7. yield 4
    8. 0 microseconds
    9. yield 5
    10. 0 microseconds
    11. yield 6
    12. 96 microseconds
    13. yield 7
    14. 99 microseconds
    15. yield 8
    16. 93 microseconds
    17. yield 9
    18. 92 microseconds
    19. yield 10
    20. 99 microseconds
    21. yield 11
    22. 2 microseconds
    23. yield 12
    24. 0 microseconds
    25. yield 13
    26. 0 microseconds
    27. yield 14
    28. 0 microseconds
    29. yield 15
    30. 41 microseconds
    31. yield 16
    32. 1 microseconds
    33. yield 17
    34. 0 microseconds
    35. yield 18
    36. 36 microseconds
    37. yield 19
    38. 0 microseconds
    39. yield 20
    40. 0 microseconds
    41. yield 21
    42. 0 microseconds
    43. yield 22
    44. 14 microseconds
    45. yield 23
    46. 14 microseconds
    47. yield 24
    48. 0 microseconds
    49. yield 25
    50. 14 microseconds
    51. yield 26
    52. 14 microseconds
    53. yield 27
    54. 0 microseconds
    55. yield 28
    56. 14 microseconds
    57. yield 29
    58. 0 microseconds
    59. yield 30
    60. 0 microseconds
    61. yield 31
    62. 14 microseconds
    63. yield 32
    64. 13 microseconds
    65. yield 33
    66. 0 microseconds
    67. yield 34
    68. 14 microseconds
    69. yield 35
    70. 14 microseconds
    71. yield 36
    72. 14 microseconds
    73. yield 37
    74. 0 microseconds
    75. yield 38
    76. 14 microseconds
    77. yield 39
    78. 1 microseconds
    79. yield 40
    80. 0 microseconds
    81. yield 41
    82. 14 microseconds
    83. yield 42
    84. 1 microseconds
    85. yield 43
    86. 0 microseconds
    87. yield 44
    88. 13 microseconds
    89. yield 45
    90. 14 microseconds
    91. yield 46
    92. 0 microseconds
    93. yield 47
    94. 14 microseconds
    95. yield 48
    96. 0 microseconds
    97. yield 49
    98. 0 microseconds
    99. yield 50
    100. 0 microseconds
    101. yield 51
    102. 14 microseconds
    103. yield 52
    104. 1 microseconds
    105. yield 53
    106. 0 microseconds
    107. yield 54
    108. 13 microseconds
    109. yield 55
    110. 1 microseconds
    111. yield 56
    112. 0 microseconds
    113. yield 57
    114. 13 microseconds
    115. yield 58
    116. 0 microseconds
    117. yield 59
    118. 0 microseconds
    119. yield 60
    120. 13 microseconds
    121. yield 61
    122. 0 microseconds
    123. yield 62
    124. 0 microseconds
    125. yield 63
    126. 14 microseconds
    127. yield 64
    128. 26 microseconds
    129. yield 65
    130. 0 microseconds
    131. yield 66
    132. 0 microseconds
    133. yield 67
    134. 14 microseconds
    135. yield 68
    136. 14 microseconds
    137. yield 69
    138. 0 microseconds
    139. yield 70
    140. 14 microseconds
    141. yield 71
    142. 0 microseconds
    143. yield 72
    144. 0 microseconds
    145. yield 73
    146. 0 microseconds
    147. yield 74
    148. 14 microseconds
    149. yield 75
    150. 0 microseconds
    151. yield 76
    152. 0 microseconds
    153. yield 77
    154. 13 microseconds
    155. yield 78
    156. 0 microseconds
    157. yield 79
    158. 0 microseconds
    159. yield 80
    160. 0 microseconds
    161. yield 81
    162. 0 microseconds
    163. yield 82
    164. 14 microseconds
    165. yield 83
    166. 14 microseconds
    167. yield 84
    168. 52 microseconds
    169. yield 85
    170. 1 microseconds
    171. yield 86
    172. 0 microseconds
    173. yield 87
    174. 14 microseconds
    175. yield 88
    176. 14 microseconds
    177. yield 89
    178. 1 microseconds
    179. yield 90
    180. 0 microseconds
    181. yield 91
    182. 24 microseconds
    183. yield 92
    184. 14 microseconds
    185. yield 93
    186. 13 microseconds
    187. yield 94
    188. 0 microseconds
    189. yield 95
    190. 14 microseconds
    191. yield 96
    192. 13 microseconds
    193. yield 97
    194. 1 microseconds
    195. yield 98
    196. 0 microseconds
    197. yield 99
    198. 14 microseconds
    199. yield 100
    200. 14 microseconds

     输出中记录的时间差长短不一,长的有数百微秒,短的则是0。对于0的情况,是因为调度器没有找到(例如相同优先级的可运行线程队列为空)其他合适的线程执行,所以马上再次执行当前线程。

    get_id

    std::this_thread::get_id的使用非常简单,它返回当前线程的id。

    1. int main()
    2. {
    3. auto tid1 = std::this_thread::get_id();
    4. std::cout << "tid1 : " << tid1 << std::endl;
    5. auto tid2 = std::this_thread::get_id();
    6. std::cout << "tid2 : " << tid2 << std::endl;
    7. }

    例如上面代码都运行在main thread中,所以tid1和tid2一定是相同的。

  • 相关阅读:
    江苏科技大学计算机考研资料汇总
    84. 柱状图中最大的矩形(hard)
    rsync远程同步
    layer 弹窗,enter 、esc 按键监听确定、取消事件
    【CSS3】CSS3 动画 ③ ( 动画属性 | CSS3 常见动画属性简介 | 动画属性简写方式 | 动画属性简写语法 | 代码示例 )
    HTTPS的工作过程
    Redis中的Hash设计和节省内存数据结构设计
    【译】Based:简单线性注意力语言模型平衡召回-吞吐量权衡
    【滤波跟踪】基于交互式多模型的UKF滤波机动目标跟踪问题研究附matlab代码
    UnityShaderLab —— 模型裁剪效果
  • 原文地址:https://blog.csdn.net/IDisposable/article/details/126305723