Boost logo

Boost Users :

Subject: [Boost-users] [boost-users][asio] why boost::thread thread1(boost::bind(...)) doesn't start io_service on multicore machine?
From: tomasz jankowski (tomasz_jacek_at_[hidden])
Date: 2009-02-05 09:16:34


I would like to test my class which implements shared data.
because of it I have written test code which use boost::thread & boost
asio (please see below).
the problem is that threads are NOT started... on multicore machine.

one one core machine it runs fine.

any idea why?

tom

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include "shared.hpp"
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>

SharedData replyToClients;

void handle_timeout1(const boost::system::error_code&)
{
  boost::minstd_rand generator(42u);
  boost::uniform_real<> uni_dist(0,1);
  boost::variate_generator<boost::minstd_rand, boost::uniform_real<>
>uni(generator, uni_dist);

  std::string key = "ello";std::cout<<key<<std::endl;
  std::string value1 = "see you later aligator";
  std::string value2 = "what do you want to eat today?";

  double cond = 0.500000;
  while(true){
    if(uni() >= cond){
      replyToClients.set(key,value1);
    }else{
      replyToClients.set(key,value2);
    }
  }
}
void handle_timeout2(const boost::system::error_code&)
{
  std::string key = "ello";

  while(true){
    std::cout<<replyToClients.get(key);
  }
}

int main(int argc, char* argv[])
{

    try{
    boost::asio::io_service io_service;
    boost::system::error_code error;
    //boost::asio::io_service::work work(io_service);

    io_service.post(boost::bind(handle_timeout1,error));
    io_service.post(boost::bind(handle_timeout2,error));
    io_service.post(boost::bind(handle_timeout2,error));
    io_service.post(boost::bind(handle_timeout2,error));

    boost::thread thread1(boost::bind(&boost::asio::io_service::run,
&io_service));
    boost::thread thread2(boost::bind(&boost::asio::io_service::run,
&io_service));
    boost::thread thread3(boost::bind(&boost::asio::io_service::run,
&io_service));
    boost::thread thread4(boost::bind(&boost::asio::io_service::run,
&io_service));

    //io_service.run();
    std::cout<<"the end\n";
    io_service.stop();
    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    }
  catch (boost::system::system_error& er)
  {std::cout << "Exception er: " << er.what() << "\n";
  }
  catch (std::exception& e)
  {
    std::cout << "Exception e: " << e.what() << "\n";
  }
  return 0;

}

----------------------------------------------------
Nu¿± Ciê utarte scenariusze?
Wymy¶l w³asn± grê flashow± i wygraj g³ówn± nagrodê 5.500 Euro:
http://klik.wp.pl/?adr=http%3A%2F%2Fwhosegame.pl%2Fcontestcard.php%3Fcontest%3D55&sid=631




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net