executors question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#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 | |
#define BOOST_THREAD_QUEUE_DEPRECATE_OLD | |
#include <boost/thread/caller_context.hpp> | |
#include <boost/thread/executors/basic_thread_pool.hpp> | |
#include <boost/thread/executors/loop_executor.hpp> | |
#include <boost/thread/executors/serial_executor.hpp> | |
#include <boost/thread/executors/inline_executor.hpp> | |
#include <boost/thread/executors/thread_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> | |
#include <boost/lexical_cast.hpp> | |
void p1(boost::mutex &mtx) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
lock.lock(); | |
std::cout << BOOST_CONTEXTOF << std::endl; | |
lock.unlock(); | |
boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); | |
} | |
void p2(boost::mutex &mtx) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
lock.lock(); | |
std::cout << BOOST_CONTEXTOF << std::endl; | |
lock.unlock(); | |
boost::this_thread::sleep_for(boost::chrono::seconds(10)); | |
} | |
int f1(boost::mutex &mtx) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
lock.lock(); | |
std::cout << BOOST_CONTEXTOF << std::endl; | |
lock.unlock(); | |
boost::this_thread::sleep_for(boost::chrono::seconds(1)); | |
return 1; | |
} | |
int f2(boost::mutex &mtx,int i) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
lock.lock(); | |
std::cout << BOOST_CONTEXTOF << std::endl; | |
lock.unlock(); | |
boost::this_thread::sleep_for(boost::chrono::seconds(2)); | |
return i + 1; | |
} | |
void submit_some(boost::executor& tp, boost::mutex &mtx) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
for (int i = 0; i < 3; ++i) { | |
tp.submit(boost::bind(p2,boost::ref(mtx))); | |
} | |
for (int i = 0; i < 3; ++i) { | |
tp.submit(boost::bind(p1,boost::ref(mtx))); | |
} | |
} | |
void at_th_entry(boost::basic_thread_pool&) | |
{ | |
} | |
int test_executor_adaptor(boost::mutex &mtx) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
lock.lock(); | |
std::cout << BOOST_CONTEXTOF << std::endl; | |
lock.unlock(); | |
{ | |
try | |
{ | |
{ | |
boost::executor_adaptor < boost::basic_thread_pool > ea(4); | |
lock.lock(); | |
std::cout << boost::this_thread::get_id() << std::endl; | |
lock.unlock(); | |
submit_some(ea, mtx); | |
{ | |
boost::future<int> t1 = boost::async(ea, &f1, boost::ref(mtx)); | |
lock.lock(); | |
std::cout << BOOST_CONTEXTOF << std::endl; | |
lock.unlock(); | |
std::cout << " t1= " << t1.get() << std::endl;//problem unknown on running showing thread result before running code | |
std::vector<boost::future<int>> vt1; | |
for (int i=0;i<4;i++) | |
{ | |
vt1.push_back(boost::async(ea, &f1, boost::ref(mtx)));//here async starts all closures already submitted to ea then submit f1 to all threads in ea to work asynchronusly so end result will be 7 already submitted and 4 more f1 and return futures to only the last 4 f1 which is submitted by async | |
} | |
for (auto &e : vt1) | |
{ | |
auto e_value = e.get(); | |
lock.lock(); | |
std::cout << e_value << std::endl; | |
lock.unlock(); | |
} | |
} | |
submit_some(ea, mtx); | |
{ | |
boost::executor_adaptor < boost::basic_thread_pool > ea3(1); | |
boost::future<int> t1 = boost::async(ea3,&f1, boost::ref(mtx)); | |
std::vector<boost::future<int>> vt1; | |
for (int i = 0;i<4;i++) | |
{ | |
vt1.push_back(boost::async(ea3, &f1, boost::ref(mtx)));//here async starts all closures already submitted to ea then submit f1 to all threads in ea to work asynchronusly so end result will be 7 already submitted and 4 more f1 and return futures to only the last 4 f1 which is submitted by async | |
} | |
for (auto &e : vt1) | |
{ | |
auto e_value = e.get(); | |
lock.lock(); | |
std::cout << e_value << std::endl; | |
lock.unlock(); | |
} | |
} | |
} | |
} | |
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() | |
{ | |
boost::mutex mtx; | |
boost::unique_lock<boost::mutex> lock(mtx, boost::defer_lock); | |
lock.lock(); | |
std::cout << boost::this_thread::get_id() << std::endl; | |
lock.unlock(); | |
return test_executor_adaptor(mtx); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment