Re: [asio-users] Strands
Brought to you by:
chris_kohlhoff
From: Niall D. <s_s...@ne...> - 2017-02-28 13:21:33
|
You'll get much faster response to bugs logged against standalone ASIO than Boost.ASIO which is now quite far away from standalone ASIO. Boost is thinking a bit harder about retiring the Trac based issue tracker entirely now they've reacquired control over Boost's servers which took over a year. Probably github issues will take over for all new issues. Niall On 28/02/2017 13:13, ivan kostov wrote: > Hi Paul, > > Thanks for the links. I will have a look. > > By the way, how shall I cope with boost issues in the future. Where is > the right place to report them ? > For example https://svn.boost.org/trac/boost/ticket/12690 > Is it the right place there ? > Because nothing happens since 2 months. And I think this is a trivial > fix to do. > > Best regards, > Ivan > > 2017-02-28 11:44 GMT+01:00 Paul <de...@uk... <mailto:de...@uk...>>: > > This issue was originally described on the stackoverflow in a question: > http://stackoverflow.com/questions/40291265/boostasio-reasoning-behind-num-implementations-for-io-servicestrand > <http://stackoverflow.com/questions/40291265/boostasio-reasoning-behind-num-implementations-for-io-servicestrand> > > with a bare minimum test case to reproduce. > > And later reported as an issue on Github. Alas with no response. > > > 28 лютого 2017, 12:04:08, від "ivan kostov" <ik...@gm... > <mailto:ik...@gm...>>: > > Hi Adam, > > thank for your response and your time. Your help is is more than > wellcome :) > > > My requirement is that execution of one strand shall not > influence the execution in another one as soon as there is a > thread available in the pool > > I have reviewed my design and removed my code, boiling it down > to the the following boost example. > > I have two strands and a thread pool with 2 threads. > > Strand2 takes longer to process ( sleeps 10 ms. ). > Strand1 processes the data without any delay. > > > When I post > > strand1 -> one, > strand2 -> three, > strand1 -> two, > > I expect that the execution order is > > One, Two, Three. > > And this is the case in most of the time. The thread processing > strand2 shall bedifferent from the thread processing strand1. > > RUN: 13443 > Processing: 'one' StrandId: '1' threadId: '139910227162880' > DONE Processing: 'one' StrandId: '1' threadId: '139910227162880' > Processing: 'two' StrandId: '1' threadId: '139910227162880' > DONE Processing: 'two' StrandId: '1' threadId: '139910227162880' > Processing: 'three' StrandId: '2' threadId: '139910218770176' > DONE Processing: 'three' StrandId: '2' threadId: '139910218770176' > > But sometimes I receive the following output > > RUN: 13444 > Processing: 'one' StrandId: '1' threadId: '139910218770176' > DONE Processing: 'one' StrandId: '1' threadId: '139910218770176' > Processing: 'three' StrandId: '2' threadId: '139910218770176' > DONE Processing: 'three' StrandId: '2' threadId: '139910218770176' > Processing: 'two' StrandId: '1' threadId: '139910218770176' > DONE Processing: 'two' StrandId: '1' threadId: '139910218770176' > ../src/playground.cpp(75): fatal error in "MyTestCase": critical > check "two" == actual[1] failed [two != three] > > Which means that the second post to Strand1 waits for the first > post on Strand2 to complete. > > Why is this happening ? Am I testing the separation in a wrong > way ? Am I using ASIO in a wrong way ? > > Please have a look into my code. > > > > Thanks a lot for your help, > Ivan > > #define BOOST_TEST_DYN_LINK > #define BOOST_TEST_MODULE MyTest > > #include <boost/test/unit_test.hpp> > #include <boost/scope_exit.hpp> > #include <boost/asio.hpp> > > #include <thread> > #include <chrono> > #include <vector> > #include <atomic> > #include <mutex> > > BOOST_AUTO_TEST_CASE(MyTestCase) > { > boost::asio::io_service service; > boost::asio::io_service::strand strand1(service); > boost::asio::io_service::strand strand2(service); > std::unique_ptr<boost::asio::io_service::work> work ( new > boost::asio::io_service::work(service)); > > std::vector<std::thread> pool; > for(int i = 0; i < 2; ++i) > { > pool.emplace_back( std::thread( [&service](){service.run(); } ) ); > } > > BOOST_SCOPE_EXIT(&work, &pool) > { > work.reset(); > for( auto &t : pool) > { > if( t.joinable()) > { > t.join(); > } > } > } > BOOST_SCOPE_EXIT_END > > for(int i = 0; i < 100000; ++i) > { > BOOST_TEST_MESSAGE("RUN: " << i); > > std::mutex mutex; > std::vector<std::string> actual; > > auto f = [&actual,&mutex](const std::string& arg, int strandId) > { > std::cout << "Processing: '" << arg << "' StrandId: '" << > strandId << "' threadId: '"<< std::this_thread::get_id()<< "'" > << std::endl; > { > std::unique_lock<std::mutex> l(mutex); > actual.emplace_back(arg); > } > std::cout << "DONE Processing: '" << arg << "' StrandId: '" << > strandId << "' threadId: '"<< std::this_thread::get_id()<< "'" > << std::endl; > > }; > > std::atomic_bool executed{false}; > strand1.post([f](){ f("one",1); }); > strand2.post([f,&executed]() > { > std::this_thread::sleep_for( std::chrono::milliseconds(10) ); > f("three",2); > executed = true; > }); > strand1.post([f](){ f("two",1); }); > > while( ! executed ) > { > std::this_thread::sleep_for( std::chrono::milliseconds(1) ); > } > > BOOST_REQUIRE_EQUAL(3,actual.size()); > BOOST_REQUIRE_EQUAL("one",actual[0]); > BOOST_REQUIRE_EQUAL("two",actual[1]); > BOOST_REQUIRE_EQUAL("three",actual[2]); > } > } > > > > > > > > > > > > > > 2017-02-23 19:38 GMT+01:00 Adam Crain <jad...@au... > <mailto:jad...@au...>>: > > If each worker is has its own strand, actions posted to > other strands can execute in parallel if you're calling > io_service::run() from multiple threads. > > -Adam > > On Thu, Feb 23, 2017 at 1:06 PM, ivan kostov > <ik...@gm... <mailto:ik...@gm...>> wrote: > > Hi all, > > I'm using asio for implementing worker/dispatchers. > > I have one dispatcher thread, receiving data from a > socket and dispatching it to one of the workers. > > Each worker object has it's own strand protecting it > from simultaneous access. > I have a pool of 5 threads serving those strands. > > If a worker takes longer to process the data and there > is a thread free in the worker's thread pool, is it > guaranteed, that other workers will be able to process > data, or it is possible that one "bussy" worker blocks > all of them ? > > > Best regards, > Ivan > > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the > world's most > engaging tech sites, SlashDot.org! http://sdm.link/slashdot > _______________________________________________ > asio-users mailing list > asi...@li... > <mailto:asi...@li...> > https://lists.sourceforge.net/lists/listinfo/asio-users > <https://lists.sourceforge.net/lists/listinfo/asio-users> > _______________________________________________ > Using Asio? List your project at > http://think-async.com/Asio/WhoIsUsingAsio > <http://think-async.com/Asio/WhoIsUsingAsio> > > > > > -- > > J Adam Crain - Partner > > <http://www.automatak.com> > > PGP 4096R/E2984A0C <https://www.automatak.com/keys/jadamcrain.asc> 2013-05-03 > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, SlashDot.org! http://sdm.link/slashdot > _______________________________________________ > asio-users mailing list > asi...@li... > <mailto:asi...@li...> > https://lists.sourceforge.net/lists/listinfo/asio-users > <https://lists.sourceforge.net/lists/listinfo/asio-users> > _______________________________________________ > Using Asio? List your project at > http://think-async.com/Asio/WhoIsUsingAsio > <http://think-async.com/Asio/WhoIsUsingAsio> > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, SlashDot.org! http://sdm.link/slashdot > > _______________________________________________ > asio-users mailing list > asi...@li... > <mailto:asi...@li...> > https://lists.sourceforge.net/lists/listinfo/asio-users > <https://lists.sourceforge.net/lists/listinfo/asio-users> > _______________________________________________ > Using Asio? List your project at > http://think-async.com/Asio/WhoIsUsingAsio > <http://think-async.com/Asio/WhoIsUsingAsio> > > > > -- > 11111 > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, SlashDot.org! http://sdm.link/slashdot > _______________________________________________ > asio-users mailing list > asi...@li... > <mailto:asi...@li...> > https://lists.sourceforge.net/lists/listinfo/asio-users > <https://lists.sourceforge.net/lists/listinfo/asio-users> > _______________________________________________ > Using Asio? List your project at > http://think-async.com/Asio/WhoIsUsingAsio > <http://think-async.com/Asio/WhoIsUsingAsio> > > > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, SlashDot.org! http://sdm.link/slashdot > > > > _______________________________________________ > asio-users mailing list > asi...@li... > https://lists.sourceforge.net/lists/listinfo/asio-users > _______________________________________________ > Using Asio? List your project at > http://think-async.com/Asio/WhoIsUsingAsio > -- ned Productions Limited Consulting http://www.nedproductions.biz/ http://ie.linkedin.com/in/nialldouglas/ |