C++11中对于多线程并发编程提供了强大的支持,在this_thread名字空间中提供了几个常用函数get_id, sleep_for, sleep_until和yield。
其中std::this_thread::yield对操作系统的调度器发出请求,主动让出执行权给其他的可运行线程。它的具体行为跟具体的实现相关,例如跟调度器算法相关。
下面简单的代码示例中循环100万次,每到一万的整数倍时主动让出执行权。
- // demonstration of std::this_thread::yield
- // by Bruce Jia
-
- #include
- #include
- #include
-
- class HighResStopwatch
- {
- private:
- std::chrono::time_point
start; -
- public:
- HighResStopwatch() noexcept: start(std::chrono::high_resolution_clock::now()) {
- }
-
- ~HighResStopwatch() noexcept {
-
- auto elapsed = std::chrono::high_resolution_clock::now() - start;
- std::cout << std::chrono::duration_cast
(elapsed).count() - << " microseconds\n";
- }
-
- HighResStopwatch(HighResStopwatch &) = delete;
- void operator = (HighResStopwatch &) = delete;
- };
-
- int main()
- {
- long maxCount = 1000000L;
- int j = 1;
-
- for (long i=1; i<=maxCount; i++) {
- if (i % 10000 == 0) {
- std::cout << "yield " << j++ << std::endl;
- HighResStopwatch watch;
- std::this_thread::yield();
- }
- }
- }
这里一个简单的类HighResStopwatch在析构的时候会打印出它的存活时间,近似等于出让执行权的时间。输出类似如下
- yield 1
- 1 microseconds
- yield 2
- 260 microseconds
- yield 3
- 0 microseconds
- yield 4
- 0 microseconds
- yield 5
- 0 microseconds
- yield 6
- 96 microseconds
- yield 7
- 99 microseconds
- yield 8
- 93 microseconds
- yield 9
- 92 microseconds
- yield 10
- 99 microseconds
- yield 11
- 2 microseconds
- yield 12
- 0 microseconds
- yield 13
- 0 microseconds
- yield 14
- 0 microseconds
- yield 15
- 41 microseconds
- yield 16
- 1 microseconds
- yield 17
- 0 microseconds
- yield 18
- 36 microseconds
- yield 19
- 0 microseconds
- yield 20
- 0 microseconds
- yield 21
- 0 microseconds
- yield 22
- 14 microseconds
- yield 23
- 14 microseconds
- yield 24
- 0 microseconds
- yield 25
- 14 microseconds
- yield 26
- 14 microseconds
- yield 27
- 0 microseconds
- yield 28
- 14 microseconds
- yield 29
- 0 microseconds
- yield 30
- 0 microseconds
- yield 31
- 14 microseconds
- yield 32
- 13 microseconds
- yield 33
- 0 microseconds
- yield 34
- 14 microseconds
- yield 35
- 14 microseconds
- yield 36
- 14 microseconds
- yield 37
- 0 microseconds
- yield 38
- 14 microseconds
- yield 39
- 1 microseconds
- yield 40
- 0 microseconds
- yield 41
- 14 microseconds
- yield 42
- 1 microseconds
- yield 43
- 0 microseconds
- yield 44
- 13 microseconds
- yield 45
- 14 microseconds
- yield 46
- 0 microseconds
- yield 47
- 14 microseconds
- yield 48
- 0 microseconds
- yield 49
- 0 microseconds
- yield 50
- 0 microseconds
- yield 51
- 14 microseconds
- yield 52
- 1 microseconds
- yield 53
- 0 microseconds
- yield 54
- 13 microseconds
- yield 55
- 1 microseconds
- yield 56
- 0 microseconds
- yield 57
- 13 microseconds
- yield 58
- 0 microseconds
- yield 59
- 0 microseconds
- yield 60
- 13 microseconds
- yield 61
- 0 microseconds
- yield 62
- 0 microseconds
- yield 63
- 14 microseconds
- yield 64
- 26 microseconds
- yield 65
- 0 microseconds
- yield 66
- 0 microseconds
- yield 67
- 14 microseconds
- yield 68
- 14 microseconds
- yield 69
- 0 microseconds
- yield 70
- 14 microseconds
- yield 71
- 0 microseconds
- yield 72
- 0 microseconds
- yield 73
- 0 microseconds
- yield 74
- 14 microseconds
- yield 75
- 0 microseconds
- yield 76
- 0 microseconds
- yield 77
- 13 microseconds
- yield 78
- 0 microseconds
- yield 79
- 0 microseconds
- yield 80
- 0 microseconds
- yield 81
- 0 microseconds
- yield 82
- 14 microseconds
- yield 83
- 14 microseconds
- yield 84
- 52 microseconds
- yield 85
- 1 microseconds
- yield 86
- 0 microseconds
- yield 87
- 14 microseconds
- yield 88
- 14 microseconds
- yield 89
- 1 microseconds
- yield 90
- 0 microseconds
- yield 91
- 24 microseconds
- yield 92
- 14 microseconds
- yield 93
- 13 microseconds
- yield 94
- 0 microseconds
- yield 95
- 14 microseconds
- yield 96
- 13 microseconds
- yield 97
- 1 microseconds
- yield 98
- 0 microseconds
- yield 99
- 14 microseconds
- yield 100
- 14 microseconds
输出中记录的时间差长短不一,长的有数百微秒,短的则是0。对于0的情况,是因为调度器没有找到(例如相同优先级的可运行线程队列为空)其他合适的线程执行,所以马上再次执行当前线程。
std::this_thread::get_id的使用非常简单,它返回当前线程的id。
- int main()
- {
- auto tid1 = std::this_thread::get_id();
- std::cout << "tid1 : " << tid1 << std::endl;
-
- auto tid2 = std::this_thread::get_id();
- std::cout << "tid2 : " << tid2 << std::endl;
- }
例如上面代码都运行在main thread中,所以tid1和tid2一定是相同的。