diff options
Diffstat (limited to 'libs/thread/example')
-rw-r--r-- | libs/thread/example/default_executor.cpp | 61 | ||||
-rw-r--r-- | libs/thread/example/fib_task_region.cpp | 91 | ||||
-rw-r--r-- | libs/thread/example/future_fallback_to.cpp | 66 | ||||
-rw-r--r-- | libs/thread/example/make_future.cpp | 20 | ||||
-rw-r--r-- | libs/thread/example/producer_consumer.cpp | 14 | ||||
-rw-r--r-- | libs/thread/example/producer_consumer2.cpp | 2 | ||||
-rw-r--r-- | libs/thread/example/serial_executor.cpp | 113 | ||||
-rw-r--r-- | libs/thread/example/serial_executor_cont.cpp | 113 | ||||
-rw-r--r-- | libs/thread/example/this_executor.cpp | 86 |
9 files changed, 552 insertions, 14 deletions
diff --git a/libs/thread/example/default_executor.cpp b/libs/thread/example/default_executor.cpp new file mode 100644 index 000000000..ccdb38767 --- /dev/null +++ b/libs/thread/example/default_executor.cpp @@ -0,0 +1,61 @@ +// Copyright (C) 2014 Vicente Botet +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/config.hpp> +#if ! defined BOOST_NO_CXX11_DECLTYPE +#define BOOST_RESULT_OF_USE_DECLTYPE +#endif + +#define BOOST_THREAD_VERSION 4 +#define BOOST_THREAD_PROVIDES_EXECUTORS +//#define BOOST_THREAD_USES_LOG +#define BOOST_THREAD_USES_LOG_THREAD_ID +#define BOOST_THREAD_QUEUE_DEPRECATE_OLD + +#include <boost/thread/caller_context.hpp> +#include <boost/thread/executors/basic_thread_pool.hpp> +#include <boost/thread/executors/generic_executor_ref.hpp> +#include <string> +#include <iostream> + +#include <boost/thread/caller_context.hpp> + + +boost::generic_executor_ref default_executor() +{ + static boost::basic_thread_pool tp(4); + return boost::generic_executor_ref(tp); +} + +void p2() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + + +void p1() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); + default_executor().submit(&p2); + boost::this_thread::sleep_for(boost::chrono::milliseconds(400)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + +int main() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + + default_executor().submit(&p1); + + boost::this_thread::sleep_for(boost::chrono::seconds(5)); + + std::cout << BOOST_CONTEXTOF << std::endl; + + return 1; + +} diff --git a/libs/thread/example/fib_task_region.cpp b/libs/thread/example/fib_task_region.cpp new file mode 100644 index 000000000..1540dc294 --- /dev/null +++ b/libs/thread/example/fib_task_region.cpp @@ -0,0 +1,91 @@ +// Copyright (C) 2012 Vicente Botet +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/config.hpp> +#if ! defined BOOST_NO_CXX11_DECLTYPE +#define BOOST_RESULT_OF_USE_DECLTYPE +#endif + +#define BOOST_THREAD_VERSION 4 +#define BOOST_THREAD_PROVIDES_EXECUTORS + +#include <boost/thread/experimental/task_region.hpp> +#include <iostream> + +#if ! defined BOOST_NO_CXX11_RANGE_BASED_FOR && ! defined BOOST_NO_CXX11_LAMBDAS + +int fib_task_region(int n) +{ + using boost::experimental::parallel::task_region; + using boost::experimental::parallel::task_region_handle; + + if (n == 0) return 0; + if (n == 1) return 1; + + int n1; + int n2; + + task_region([&](task_region_handle& trh) + { + trh.run([&] + { + n1 = fib_task_region(n - 1); + }); + + n2 = fib_task_region(n - 2); + }); + + return n1 + n2; +} + +#if defined BOOST_THREAD_PROVIDES_EXECUTORS +template <class Ex> +int fib_task_region_gen( Ex& ex, int n) +{ + using boost::experimental::parallel::task_region; + using boost::experimental::parallel::task_region_handle_gen; + + if (n == 0) return 0; + if (n == 1) return 1; + + int n1; + int n2; + + task_region(ex, [&](task_region_handle_gen<Ex>& trh) + { + trh.run([&] + { + n1 = fib_task_region(n - 1); + }); + + n2 = fib_task_region(n - 2); + }); + + return n1 + n2; +} +#endif + +int main() +{ + for (int i = 0; i<10; ++i) { + std::cout << fib_task_region(i) << " "; + } + std::cout << std::endl; + +#if defined BOOST_THREAD_PROVIDES_EXECUTORS + boost::basic_thread_pool tp; + for (int i = 0; i<10; ++i) { + std::cout << fib_task_region_gen(tp,i) << " "; + } + std::cout << std::endl; +#endif + return 0; +} +#else +int main() +{ + return 0; +} +#endif diff --git a/libs/thread/example/future_fallback_to.cpp b/libs/thread/example/future_fallback_to.cpp index d1a3c52fd..7301b0ae5 100644 --- a/libs/thread/example/future_fallback_to.cpp +++ b/libs/thread/example/future_fallback_to.cpp @@ -35,31 +35,83 @@ int p1() int main() { - const int number_of_tests = 100; + const int number_of_tests = 200; BOOST_THREAD_LOG << "<MAIN" << BOOST_THREAD_END_LOG; + + { + for (int i=0; i< number_of_tests; i++) + try + { + BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; + boost::future<int> f1 = boost::async(boost::launch::async, &p1); + BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; + f1.wait(); + BOOST_ASSERT(f1.get()==1); + BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; + } + catch (std::exception& ex) + { + std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; + BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; + return 1; + } + catch (...) + { + std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; + BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; + return 2; + } + } + { for (int i=0; i< number_of_tests; i++) try { - //boost::future<int> f1 = boost::async(boost::launch::async, &p1); BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; boost::future<int> f1 = boost::async(&p1); BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; boost::future<int> f2 = f1.fallback_to(-1); BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; f2.wait(); + //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; BOOST_ASSERT(f2.get()==1); + //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; + BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; + } + catch (std::exception& ex) + { + std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; + BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; + return 1; + } + catch (...) + { + std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; + BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; + return 2; + } + } + + { + for (int i=0; i< number_of_tests; i++) + try + { + BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; + boost::future<int> f1 = boost::async(boost::launch::async, &p1_ex); + BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; + f1.wait(); + BOOST_ASSERT(f1.get_or(-1)==-1); BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; } catch (std::exception& ex) { - std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl; + std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; return 1; } catch (...) { - std::cout << " ERRORRRRR exception thrown" << std::endl; + std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; return 2; } @@ -75,18 +127,20 @@ int main() boost::future<int> f2 = f1.fallback_to(-1); BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; f2.wait(); + //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; BOOST_ASSERT(f2.get()==-1); + //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; } catch (std::exception& ex) { - std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl; + std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; return 1; } catch (...) { - std::cout << " ERRORRRRR exception thrown" << std::endl; + std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; return 2; } diff --git a/libs/thread/example/make_future.cpp b/libs/thread/example/make_future.cpp index 399bb46f5..3b427e39f 100644 --- a/libs/thread/example/make_future.cpp +++ b/libs/thread/example/make_future.cpp @@ -104,6 +104,26 @@ int main() boost::future<int&> f = compute_ref(0); std::cout << f.get() << std::endl; } +#if __cplusplus > 201103L + { + std::cout << __FILE__ << " "<< __LINE__ << std::endl; + int i = 0; + boost::future<int&> f = boost::make_ready_future(std::ref(i)); + std::cout << f.get() << std::endl; + } +#endif + { + std::cout << __FILE__ << " "<< __LINE__ << std::endl; + int i = 0; + boost::future<int&> f = boost::make_ready_future(boost::ref(i)); + std::cout << f.get() << std::endl; + } + { + std::cout << __FILE__ << " "<< __LINE__ << std::endl; + const int i = 0; + boost::future<int const&> f = boost::make_ready_future(boost::cref(i)); + std::cout << f.get() << std::endl; + } // { // std::cout << __FILE__ << " "<< __LINE__ << std::endl; // boost::future<int> f = compute(2); diff --git a/libs/thread/example/producer_consumer.cpp b/libs/thread/example/producer_consumer.cpp index fb6e16b35..51979b6c7 100644 --- a/libs/thread/example/producer_consumer.cpp +++ b/libs/thread/example/producer_consumer.cpp @@ -22,7 +22,7 @@ typedef std::ostream the_ostream; typedef std::istream the_istream; #endif -#include <boost/thread/sync_queue.hpp> +#include <boost/thread/concurrent_queues/sync_queue.hpp> void producer(the_ostream &mos, boost::sync_queue<int> & sbq) { @@ -30,9 +30,9 @@ void producer(the_ostream &mos, boost::sync_queue<int> & sbq) try { for(int i=0; ;++i) { - sbq.push_back(i); + sbq.push(i); //sbq << i; - mos << "push_back(" << i << ") "<< sbq.size()<<"\n"; + mos << "push(" << i << ") "<< sbq.size()<<"\n"; this_thread::sleep_for(chrono::milliseconds(200)); } } @@ -55,7 +55,7 @@ void consumer( for(int i=0; ;++i) { int r; - sbq.pull_front(r); + sbq.pull(r); //sbq >> r; mos << i << " pull(" << r << ") "<< sbq.size()<<"\n"; @@ -78,7 +78,7 @@ void consumer2(the_ostream &mos, boost::sync_queue<int> & sbq) for(int i=0; ;++i) { int r; - queue_op_status st = sbq.try_pull_front(r); + queue_op_status st = sbq.try_pull(r); if (queue_op_status::closed == st) break; if (queue_op_status::success == st) { mos << i << " pull(" << r << ")\n"; @@ -98,9 +98,9 @@ void consumer3(the_ostream &mos, boost::sync_queue<int> & sbq) for(int i=0; ;++i) { int r; - queue_op_status res = sbq.wait_pull_front(r); + queue_op_status res = sbq.wait_pull(r); if (res==queue_op_status::closed) break; - mos << i << " wait_pull_front(" << r << ")\n"; + mos << i << " wait_pull(" << r << ")\n"; this_thread::sleep_for(chrono::milliseconds(250)); } } diff --git a/libs/thread/example/producer_consumer2.cpp b/libs/thread/example/producer_consumer2.cpp index 110cbbab3..9e3ad976c 100644 --- a/libs/thread/example/producer_consumer2.cpp +++ b/libs/thread/example/producer_consumer2.cpp @@ -21,7 +21,7 @@ typedef std::ostream the_ostream; typedef std::istream the_istream; #endif -#include <boost/thread/sync_queue.hpp> +#include <boost/thread/concurrent_queues/sync_queue.hpp> #include <boost/thread/concurrent_queues/queue_adaptor.hpp> #include <boost/thread/concurrent_queues/queue_views.hpp> #include <boost/static_assert.hpp> diff --git a/libs/thread/example/serial_executor.cpp b/libs/thread/example/serial_executor.cpp new file mode 100644 index 000000000..ad25e822e --- /dev/null +++ b/libs/thread/example/serial_executor.cpp @@ -0,0 +1,113 @@ +// Copyright (C) 2015 Vicente J. Botet Escriba +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/config.hpp> +#if ! defined BOOST_NO_CXX11_DECLTYPE +#define BOOST_RESULT_OF_USE_DECLTYPE +#endif + +#define BOOST_THREAD_VERSION 4 +#define BOOST_THREAD_PROVIDES_EXECUTORS +//#define BOOST_THREAD_USES_LOG +#define BOOST_THREAD_USES_LOG_THREAD_ID +#define BOOST_THREAD_QUEUE_DEPRECATE_OLD + +#include <boost/thread/caller_context.hpp> +#include <boost/thread/executors/basic_thread_pool.hpp> +#include <boost/thread/executors/serial_executor.hpp> +#include <boost/thread/executors/executor.hpp> +#include <boost/thread/executors/executor_adaptor.hpp> +#include <boost/thread/executor.hpp> +#include <boost/thread/future.hpp> +#include <boost/assert.hpp> +#include <string> +#include <iostream> + +void p1() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(30)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + +void p2() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + +int f1() +{ + // std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::seconds(1)); + return 1; +} +int f2(int i) +{ + // std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::seconds(2)); + return i + 1; +} + +void submit_some(boost::serial_executor& tp) +{ + std::cout << BOOST_CONTEXTOF << std::endl; + for (int i = 0; i < 3; ++i) { + std::cout << BOOST_CONTEXTOF << std::endl; + tp.submit(&p2); + } + for (int i = 0; i < 3; ++i) { + std::cout << BOOST_CONTEXTOF << std::endl; + tp.submit(&p1); + } + std::cout << BOOST_CONTEXTOF << std::endl; + +} + + +void at_th_entry(boost::basic_thread_pool& ) +{ + +} + +int test_executor_adaptor() +{ + // std::cout << BOOST_CONTEXTOF << std::endl; + { + try + { + +#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + // std::cout << BOOST_CONTEXTOF << std::endl; + { + boost::basic_thread_pool ea1(4); + boost::serial_executor ea2(ea1); + submit_some(ea2); + boost::this_thread::sleep_for(boost::chrono::seconds(10)); + } +#endif + // std::cout << BOOST_CONTEXTOF << std::endl; + } + catch (std::exception& ex) + { + std::cout << "ERROR= " << ex.what() << "" << std::endl; + return 1; + } + catch (...) + { + std::cout << " ERROR= exception thrown" << std::endl; + return 2; + } + } + // std::cout << BOOST_CONTEXTOF << std::endl; + return 0; +} + + +int main() +{ + return test_executor_adaptor(); +} diff --git a/libs/thread/example/serial_executor_cont.cpp b/libs/thread/example/serial_executor_cont.cpp new file mode 100644 index 000000000..1883b0718 --- /dev/null +++ b/libs/thread/example/serial_executor_cont.cpp @@ -0,0 +1,113 @@ +// Copyright (C) 2015 Vicente J. Botet Escriba +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/config.hpp> +#if ! defined BOOST_NO_CXX11_DECLTYPE +#define BOOST_RESULT_OF_USE_DECLTYPE +#endif + +#define BOOST_THREAD_VERSION 4 +#define BOOST_THREAD_PROVIDES_EXECUTORS +//#define BOOST_THREAD_USES_LOG +#define BOOST_THREAD_USES_LOG_THREAD_ID +#define BOOST_THREAD_QUEUE_DEPRECATE_OLD + +#include <boost/thread/caller_context.hpp> +#include <boost/thread/executors/basic_thread_pool.hpp> +#include <boost/thread/executors/serial_executor_cont.hpp> +#include <boost/thread/executors/executor.hpp> +#include <boost/thread/executors/executor_adaptor.hpp> +#include <boost/thread/executor.hpp> +#include <boost/thread/future.hpp> +#include <boost/assert.hpp> +#include <string> +#include <iostream> + +void p1() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(30)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + +void p2() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + +int f1() +{ + // std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::seconds(1)); + return 1; +} +int f2(int i) +{ + // std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::seconds(2)); + return i + 1; +} + +void submit_some(boost::serial_executor_cont& tp) +{ + std::cout << BOOST_CONTEXTOF << std::endl; + for (int i = 0; i < 3; ++i) { + std::cout << BOOST_CONTEXTOF << std::endl; + tp.submit(&p2); + } + for (int i = 0; i < 3; ++i) { + std::cout << BOOST_CONTEXTOF << std::endl; + tp.submit(&p1); + } + std::cout << BOOST_CONTEXTOF << std::endl; + +} + + +void at_th_entry(boost::basic_thread_pool& ) +{ + +} + +int test_executor_adaptor() +{ + // std::cout << BOOST_CONTEXTOF << std::endl; + { + try + { + +#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + // std::cout << BOOST_CONTEXTOF << std::endl; + { + boost::basic_thread_pool ea1(4); + boost::serial_executor_cont ea2(ea1); + submit_some(ea2); + boost::this_thread::sleep_for(boost::chrono::seconds(10)); + } +#endif + // std::cout << BOOST_CONTEXTOF << std::endl; + } + catch (std::exception& ex) + { + std::cout << "ERROR= " << ex.what() << "" << std::endl; + return 1; + } + catch (...) + { + std::cout << " ERROR= exception thrown" << std::endl; + return 2; + } + } + // std::cout << BOOST_CONTEXTOF << std::endl; + return 0; +} + + +int main() +{ + return test_executor_adaptor(); +} diff --git a/libs/thread/example/this_executor.cpp b/libs/thread/example/this_executor.cpp new file mode 100644 index 000000000..feeebf854 --- /dev/null +++ b/libs/thread/example/this_executor.cpp @@ -0,0 +1,86 @@ +// Copyright (C) 2014 Vicente Botet +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/config.hpp> +#if ! defined BOOST_NO_CXX11_DECLTYPE +#define BOOST_RESULT_OF_USE_DECLTYPE +#endif + +#define BOOST_THREAD_VERSION 4 +#define BOOST_THREAD_PROVIDES_EXECUTORS +#define BOOST_THREAD_USES_LOG_THREAD_ID + +#include <boost/thread/caller_context.hpp> +#include <boost/thread/executors/basic_thread_pool.hpp> +#include <boost/thread/executors/generic_executor_ref.hpp> +#include <boost/smart_ptr/shared_ptr.hpp> +#include <boost/smart_ptr/make_shared.hpp> +#include <string> +#include <iostream> + +#include <boost/thread/caller_context.hpp> + +struct current_executor_state_type { + boost::shared_ptr<boost::generic_executor_ref> current_executor_ptr; + + template <class Executor> + void set_current_executor(Executor& ex) + { + current_executor_ptr = boost::make_shared<boost::generic_executor_ref>(ex); + } + boost::generic_executor_ref current_executor() + { + if (current_executor_ptr) + return *current_executor_ptr; + else + throw ""; + } +}; + +thread_local current_executor_state_type current_executor_state; + +boost::generic_executor_ref current_executor() +{ + return current_executor_state.current_executor(); +} + +void p2() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + + +void p1() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); + current_executor().submit(&p2); + boost::this_thread::sleep_for(boost::chrono::milliseconds(400)); + std::cout << BOOST_CONTEXTOF << std::endl; +} + +int main() +{ + std::cout << BOOST_CONTEXTOF << std::endl; + + boost::basic_thread_pool tp(4, + // at_thread_entry + [](boost::basic_thread_pool& pool) + { + current_executor_state.set_current_executor(pool); + } + ); + + tp.submit(&p1); + + boost::this_thread::sleep_for(boost::chrono::seconds(5)); + + std::cout << BOOST_CONTEXTOF << std::endl; + + return 1; + +} |