Boost thread synchronization in release build
- by Joseph16
Hi, when I try to run the following code in debug and release mode in VS2005. Each time I see different output in console and It doesn't seem like the multithreading is achieved in release mode.
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. }
Debug Mode
Thread 00153E60: 0
Thread 00153E90: 0
Thread 00153E60: 1
Thread 00153E90: 1
Thread 00153E90: 2
Thread 00153E60: 2
Thread 00153E90: 3
Thread 00153E60: 3
Thread 00153E60: 4
Thread 00153E90: 4
Press any key to continue . . .
Release Mode
Thread 00153D28: 0
Thread 00153D28: 1
Thread 00153D28: 2
Thread 00153D28: 3
Thread 00153D28: 4
Thread 00153D58: 0
Thread 00153D58: 1
Thread 00153D58: 2
Thread 00153D58: 3
Thread 00153D58: 4
Press any key to continue . . .