From de616b29c548fbbd8c629ca2ca2d139fabee12a1 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 18 Jan 2015 02:06:18 +0000 Subject: [PATCH] fix c++98 support --- src/random.cpp | 30 ++++++++++++++++++++++++++---- test/test_upnp.cpp | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/random.cpp b/src/random.cpp index 0e6d61753..96d4811a9 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -39,15 +39,37 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { + using boost::random::random_device; + using boost::random::mt19937; + using boost::random::uniform_int_distribution; + +#if __cplusplus < 199711L + // because local statics are not atomic pre c++11 + // do it manually, probably at a higher cost + namespace + { + static mutex random_device_mutex; + static random_device* dev = NULL; + static mt19937* rnd = NULL; + } +#endif + boost::uint32_t random() { - using boost::random::random_device; - using boost::random::mt19937; - using boost::random::uniform_int_distribution; - +#if __cplusplus >= 199711L static random_device dev; static mt19937 random_engine(dev()); return uniform_int_distribution(0, UINT_MAX)(random_engine); +#else + mutex::scoped_lock l(random_device_mutex); + + if (dev == NULL) + { + dev = new random_device(); + rnd = new mt19937((*dev)()); + } + return uniform_int_distribution(0, UINT_MAX)(*rnd); +#endif } } diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index 992238b52..1b673f36d 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -151,7 +151,7 @@ int run_upnp_test(char const* root_filename, char const* router_model, char cons std::string user_agent = "test agent"; - boost::shared_ptr upnp_handler = boost::make_shared(ios + boost::shared_ptr upnp_handler = boost::make_shared(boost::ref(ios) , address_v4::from_string("127.0.0.1") , user_agent, &callback, &log_callback, false); upnp_handler->start();