#include <cassert> #include <iostream> #include <boost/ref.hpp> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/atomic.hpp> using namespace std; using namespace boost; void double_int(int & i){ i *= 2; } mutex io_mu; void printing(atomic_int& x, const string& str){ for(int i=0;i<5;++i){ mutex::scoped_lock lock(io_mu); cout<< str<<++x<<endl; this_thread::sleep_for(chrono::seconds(1)); } } int main(){ atomic_int x(0); thread(printing, boost::ref(x), "hello"); thread(printing, boost::ref(x), "boost"); this_thread::sleep_for(chrono::seconds(2)); /* int i = 10; cout<<i<<endl; double_int(ref(i)); cout<<i<<endl; */ /* int x = 10; reference_wrapper<int> rw(x); assert( x == rw); (int &)rw = 100; cout<<"rw="<<rw<<endl; cout<<"x="<<x<<endl; reference_wrapper<int> rw2(rw); (int &)rw2 = 101; cout<<"rw="<<rw<<endl; cout<<"x="<<x<<endl; auto rw3 = ref(x); cout<< typeid(rw3).name()<<endl; auto rw4 = cref(x); cout<< typeid(rw4).name()<<endl; */ std::system("pause"); return 0; }
boost thread
时间: 2024-12-20 22:01:09