From f024849061ad3fecaa7d50551f8379c9b01ecae6 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Mon, 11 Apr 2011 17:29:23 +0000 Subject: [PATCH] common: added MultiVersion template. --- libs/libcommon/src/tests/multi_version.cpp | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 libs/libcommon/src/tests/multi_version.cpp diff --git a/libs/libcommon/src/tests/multi_version.cpp b/libs/libcommon/src/tests/multi_version.cpp new file mode 100644 index 00000000000..679818b88b1 --- /dev/null +++ b/libs/libcommon/src/tests/multi_version.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + + +typedef std::string T; +typedef Yandex::MultiVersion MV; +typedef std::vector Results; + + +void thread1(MV & x, T & result) +{ + MV::Version v = x.get(); + result = *v; +} + +void thread2(MV & x, const char * result) +{ + x.set(new T(result)); +} + + +int main(int argc, char ** argv) +{ + try + { + const char * s1 = "Hello!"; + const char * s2 = "Goodbye!"; + + size_t n = 1000; + MV x(new T(s1)); + Results results(n); + + boost::threadpool::pool tp(8); + for (size_t i = 0; i < n; ++i) + { + tp.schedule(boost::bind(thread1, boost::ref(x), boost::ref(results[i]))); + tp.schedule(boost::bind(thread2, boost::ref(x), (rand() % 2) ? s1 : s2)); + } + tp.wait(); + + for (size_t i = 0; i < n; ++i) + std::cerr << results[i] << " "; + std::cerr << std::endl; + } + catch (const Poco::Exception & e) + { + std::cerr << e.message() << std::endl; + throw; + } + + return 0; +}