今天看了篇介绍condition_variable的文章。因而copy例子到IDE下运行看看,小改了个地方,就出现了让我百思不得姐的结果。 程序以下:ios
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex, std::unique_lock #include <condition_variable> // std::condition_variable std::mutex mtx; // 全局互斥锁. std::condition_variable cv; // 全局条件变量. bool ready = false; // 全局标志位. void do_print_id(int id) { int local = id; std::unique_lock <std::mutex> lck(mtx); while (!ready) {// 若是标志位不为 true, 则等待... cv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 以后, local++; //这句到底执行仍是不执行? } // 线程被唤醒, 继续往下执行打印线程编号id. std::cout << "thread " << id << "\tlocal " << local << std::endl; } void go() { std::unique_lock <std::mutex> lck(mtx); ready = true; // 设置全局标志位为 true. cv.notify_all(); // 唤醒全部线程. std::cout << "notify_all\n"; } int main() { const int threads_num = 5; //改变此常数,结果就发生变化了~ std::thread threads[threads_num]; // spawn 10 threads: for (int i = 0; i < threads_num; ++i) threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n"; go(); // go! for (auto & th:threads) th.join(); return 0; }
上述程序中,改变线程数,即
const int threads_num = 5;
结果就发生变化了~ 当threads_num<=5时结果以下:测试
10 threads ready to race... notify_all thread 4 local 4 thread 1 local 2 thread 0 local 0 thread 2 local 2 thread 3 local 3spa
当threads_num>=10,大部分时候程序运行结果以下:线程
10 threads ready to race... notify_all thread 5 local 6 thread 0 local 1 thread 3 local 4 thread 2 local 3 thread 1 local 2 thread 7 local 8 thread 4 local 5 thread 6 local 7 thread 8 local 9 thread 9 local 10code
当threads_num介于5和10之间时,结果在二者之间。编译器
基本上就是当线程数较小时, cv.wait(lck);后面的那句local++; 不执行;当线程数较大时,又执行了…… 按道理来说,cv.wait(lck);以后线程阻塞,那么当cv.notify_all();唤醒全部线程以后应该从阻塞的地方开始继续执行才对,就应该每次都会执行后面的local++; 为什么程序行为会不一致呢?it
程序的运行环境是Xcode6.1。系统默认编译器。io
哪位大神帮忙看一下是什么缘由?编译
——————————————————后续的分割线———————————————————— 多谢 @BrotherClose 的点拨。经过这个还有几个发现:thread