Boost logo

Boost Users :

From: Stephen Torri (storri_at_[hidden])
Date: 2008-08-18 15:50:11


What I am trying to learn how to do is convert a single threaded program
into a multi-threaded program. The task of the program is to analyze a
text file a set number of times. Each analysis of the text file does not
depend on another. The only thing that they need is the name of the
file, how many iterations of the analysis to do and a unique id. I am
not concerned about the information from each round being printed in
sequential order. That is for example if thread A has a unique ID of 50
and thread B has a unique ID of 51 that I want thread B's output ahead
of thread A's output. The output is going to be captured into a file and
post-processed in a spreadsheet application.

Below is a running example of what I am trying to do.

To restate my question:

        How do I use boost threads to make a multi-threaded version of
        this program?

Stephen

----- EXAMPLE CODE -----

#include <boost/bind.hpp>
#include <boost/cstdint.hpp>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>

#include <iostream>
#include <string>
#include <sstream>

class Work {
public:

  std::string process ( boost::uint32_t round_number, boost::uint32_t iterations, std::string file )
  {
    // Read file for data
    std::stringstream result;

    for ( boost::uint32_t index = 0;
          index != iterations;
          ++index )
      {
        // Perform algorithm and save best result
        result << boost::format ("%1%:%2%")
          % round_number
          % index << std::endl;
      }

    return result.str();
  }

};

int main (int argc, char** argv)
{
  boost::program_options::options_description generic ( "Generic options" );
  generic.add_options()
    ( "help", "produce help message" )
    ( "file", boost::program_options::value<std::string>(), "Target file" )
    ( "rounds", boost::program_options::value<boost::uint32_t>(), "Number of rounds (default: 1)" )
    ( "iterations", boost::program_options::value<boost::uint32_t>(), "Number of iterations (default: 5)" );

  boost::program_options::variables_map vm;
  boost::program_options::store ( boost::program_options::command_line_parser ( argc, argv ).
                                  options(generic).run(), vm );
  boost::program_options::notify ( vm );

  if ( vm.count ( "help" ) )
    {
      std::cout << generic << std::endl;
      return 0;
    }

  // Get values from command flags
  boost::uint32_t m_rounds = 1;
  boost::uint32_t m_iterations = 5;

  if ( vm.count ( "rounds" ) )
    {
      m_rounds = vm["rounds"].as<boost::uint32_t>();
      if ( m_rounds == 0 )
        {
          std::cout << " Invalid number of rounds: 0" << std::endl;
          return 0;
        }
    }

  if ( vm.count ( "iterations" ) )
    {
      m_iterations = vm["iterations"].as<boost::uint32_t>();
      if ( m_iterations == 0 )
        {
          std::cout << " Invalid number of iterations: 0" << std::endl;
          return 0;
        }
    }

  std::cout << "Number of rounds: " << m_rounds << std::endl;
  std::cout << "Number of iterations: " << m_iterations << std::endl;

  boost::shared_ptr<Work> data_alg_ptr ( new Work() );

  for ( boost::uint32_t index = 0;
        index < m_rounds;
        ++index )
    {
      std::cout << data_alg_ptr->process ( index, m_iterations, "data.txt" ) << std::endl;
    }

  
  return 0;
}


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