4
任何人都可以告诉这里发生了什么?当我尝试调试代码时,以及控件位于第15行的thread()函数中时,它会跳过第16行移动到第17行并返回第16行。为什么它不会逐行移动?Boost多线程
1. #include <boost/thread.hpp>
2. #include <iostream>
3.
4. void wait(int seconds)
5. {
6. boost::this_thread::sleep(boost::posix_time::seconds(seconds));
7. }
8.
9. boost::mutex mutex;
10.
11. void thread()
12. {
13. for (int i = 0; i < 5; ++i)
14. {
15. wait(1);
16. mutex.lock();
17. std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
18. mutex.unlock();
19. }
20. }
21.
22. int main()
23. {
24. boost::thread t1(thread);
25. boost::thread t2(thread);
26. t1.join();
27. t2.join();
28. }