1 // Copyright (C) 2015 Vicente Botet 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #define BOOST_THREAD_VERSION 4 7 #include <boost/thread.hpp> 8 #include <boost/thread/tss.hpp> 9 #include <boost/shared_ptr.hpp> 10 #include <iostream> 11 12 struct A 13 { DoWorkA14 void DoWork() 15 { 16 std::cout << "A: doing work\n"; 17 if (!m_ptr.get()) 18 m_ptr.reset(new WorkSpace()); 19 // do not very much 20 for (size_t i = 0; i < 10; ++i) 21 m_ptr->a += 10; 22 } 23 24 private: 25 struct WorkSpace 26 { 27 int a; WorkSpaceA::WorkSpace28 WorkSpace() : a(0) {} 29 }; 30 boost::thread_specific_ptr<WorkSpace> m_ptr; 31 }; 32 33 struct B 34 { DoWorkB35 void DoWork() 36 { 37 std::cout << "B: doing work\n"; 38 if (!m_ptr.get()) 39 m_ptr.reset(new A()); 40 m_ptr->DoWork(); 41 } 42 private: 43 boost::thread_specific_ptr<A> m_ptr; 44 }; 45 46 struct C 47 { DoWorkC48 void DoWork() 49 { 50 std::cout << "C: doing work\n"; 51 if (!m_ptr.get()) 52 m_ptr.reset(new B()); 53 m_ptr->DoWork(); 54 } 55 private: 56 boost::thread_specific_ptr<B> m_ptr; 57 }; 58 main(int ac,char ** av)59int main(int ac, char** av) 60 { 61 std::cout << "test starting\n"; 62 boost::shared_ptr<C> p_C(new C); 63 boost::thread cWorker(&C::DoWork, p_C); 64 cWorker.join(); 65 std::cout << "test stopping\n"; 66 return 0; 67 } 68 69 70 71