| 1 | #include <string.h> |
|---|---|
| 2 | #include <iostream> |
| 3 | #include <Common/ThreadPool.h> |
| 4 | #include <functional> |
| 5 | #include <Common/MultiVersion.h> |
| 6 | #include <Poco/Exception.h> |
| 7 | |
| 8 | |
| 9 | using T = std::string; |
| 10 | using MV = MultiVersion<T>; |
| 11 | using Results = std::vector<T>; |
| 12 | |
| 13 | |
| 14 | static void thread1(MV & x, T & result) |
| 15 | { |
| 16 | MV::Version v = x.get(); |
| 17 | result = *v; |
| 18 | } |
| 19 | |
| 20 | static void thread2(MV & x, const char * result) |
| 21 | { |
| 22 | x.set(std::make_unique<T>(result)); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | int main(int, char **) |
| 27 | { |
| 28 | try |
| 29 | { |
| 30 | const char * s1 = "Hello!"; |
| 31 | const char * s2 = "Goodbye!"; |
| 32 | |
| 33 | size_t n = 1000; |
| 34 | MV x(std::make_unique<T>(s1)); |
| 35 | Results results(n); |
| 36 | |
| 37 | ThreadPool tp(8); |
| 38 | for (size_t i = 0; i < n; ++i) |
| 39 | { |
| 40 | tp.scheduleOrThrowOnError(std::bind(thread1, std::ref(x), std::ref(results[i]))); |
| 41 | tp.scheduleOrThrowOnError(std::bind(thread2, std::ref(x), (rand() % 2) ? s1 : s2)); |
| 42 | } |
| 43 | tp.wait(); |
| 44 | |
| 45 | for (size_t i = 0; i < n; ++i) |
| 46 | std::cerr << results[i] << " "; |
| 47 | std::cerr << std::endl; |
| 48 | } |
| 49 | catch (const Poco::Exception & e) |
| 50 | { |
| 51 | std::cerr << e.message() << std::endl; |
| 52 | throw; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 |