Boost logo

Boost Users :

From: Sliwa, Przemyslaw (London) (Przemyslaw_Sliwa_at_[hidden])
Date: 2005-04-18 12:29:50


I see the mailing program did not attach the code.

Here it is:

#pragma once

#include <string>
#include <fstream>

class CFXTrade
{
public:
        CFXTrade();
        CFXTrade(int, const std::string&, double);
        virtual ~CFXTrade();
public:
        int m_TradeID;
        std::string m_Name;
        double m_Amount;

public:
        // Write the attributes to file
        void getWrittenToDisk(std::ofstream&);
};

#include ".\fxtrade.h"
#include <cmath>

using namespace std;

CFXTrade::CFXTrade()
{
}

CFXTrade::CFXTrade(int nTradeID, const std::string& nName, double
nAmount)
        : m_TradeID(nTradeID), m_Name(nName), m_Amount(nAmount) {}

CFXTrade::~CFXTrade() {}

// Write the attributes to file
void CFXTrade::getWrittenToDisk(ofstream& nOutput)
{
        nOutput << m_TradeID << ";" << m_Name << ";" << m_Amount <<
endl;
}

#include "FXTrade.h"
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <vector>
#include <iostream>
#include <cmath>
#include <exception>
#include <boost/random.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost;

namespace write_namespace
{
        mutex file_mutex;
        ofstream out;
        const size_t dimension(10000);
        const size_t size(10);
        const unsigned int number_of_threads(10);
}

queue<CFXTrade> TradeVector;

namespace read_namespace
{
        mutex in_file_mutex;
        ifstream in;
}

void write_to_disk(const vector<CFXTrade>& nVector)
{

        using namespace write_namespace;

        mutex::scoped_lock lock(file_mutex);
        
        for(vector<CFXTrade>::const_iterator i_ptr=nVector.begin();
i_ptr!=nVector.end(); ++i_ptr)
        {
                out << i_ptr->m_TradeID << ";" << i_ptr->m_Name << ";"
<< i_ptr->m_Amount << endl;
        }

}

auto_ptr<CFXTrade> getTradeFromString(string& c_tmp)
{

        int i_TradeID;
        string c_Name, c_sub;
        string::size_type pos;
        double d_Amount(0.0);

        pos = c_tmp.find(";");
        c_sub = c_tmp.substr(0, pos);
        i_TradeID = atoi(c_sub.c_str());

        c_sub = c_tmp.substr(++pos, 3);
        c_Name = c_sub;

        c_sub = c_tmp.substr(pos+4, 3);
        
        d_Amount = atof(c_sub.c_str());
        d_Amount = 100.;

        return auto_ptr<CFXTrade>(new CFXTrade(i_TradeID, c_Name,
d_Amount));

}

void read_from_disk(queue<CFXTrade>& nTradeVector)
{
        using namespace write_namespace;
        using namespace read_namespace;
        
        string c_tmp;

        try
        {
                mutex::scoped_lock lock(in_file_mutex);

                for(size_t i=0; i<dimension; ++i)
                {
                        getline(in, c_tmp);
                        TradeVector.push(*getTradeFromString(c_tmp) );
                }
        }

        catch(exception& e)
        {
                mutex::scoped_lock lock(in_file_mutex);

                cout << "A standard exception caught from " << __LINE__
<< endl;
                cout << e.what() << endl;
        }

}

bool generate_file()
{

        using namespace write_namespace;

        vector< thread* > ThreadVector(number_of_threads);

        mt19937 mt2;
        random_number_generator<mt19937> std_rng(mt2);

        int TradeIDVector[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        char* NameVector[] = {"ABC", "BCD", "CDE", "DEF", "EFG", "FGH",
"GHI", "HIJ", "IJK", "JKL"};
        double AmountVector[] = {10., 20., 30., 40., 50., 60., 70., 80.,
90., 11.};

        vector< vector<CFXTrade> > TradeVector(number_of_threads);

        for(int i=0; i<number_of_threads; ++i)
        {
                for(int j=0; j<dimension; ++j)
                {
        
TradeVector[i].push_back(CFXTrade(TradeIDVector[std_rng(size)],
string(NameVector[std_rng(size)]),
        
AmountVector[std_rng(size)]));
                }
        }

        out.open("file.txt");

        for(int i=0; i<number_of_threads; ++i)
        {
                ThreadVector[i] = new thread(bind(&write_to_disk,
TradeVector[i]));
        }

        for(int i=0; i<number_of_threads; ++i)
        {
                ThreadVector[i]->join();
        }

        for(int i=0; i<number_of_threads; ++i)
        {
                delete ThreadVector[i];
        }

        out.close();

        return true;
}

bool read_trades()
{
        using namespace write_namespace;
        using namespace read_namespace;

        vector<thread*> ReadThreadVector(number_of_threads);

        in.open("file.txt");

        try
        {
                for(int i=0; i<number_of_threads; ++i)
                {
                        ReadThreadVector[i] = new
thread(bind(&read_from_disk, TradeVector));
                }

                for(int i=0; i<number_of_threads; ++i)
                {
                        ReadThreadVector[i]->join();
                }

                for(int i=0; i<number_of_threads; ++i)
                {
                        delete ReadThreadVector[i];
                }
        }
        catch(exception& e)
        {
                cout << "A standard exception caught from " << __LINE__
<< endl;
                cout << e.what() << endl;

                return false;
        }

        in.close();

        return true;
}

void classified()
{
        using namespace read_namespace;
        try
        {
                generate_file();
                read_trades();
        }

        catch(exception& e)
        {
                cout << e.what() << endl;
                exit(EXIT_FAILURE);
        }
        catch(...)
        {
                cout << "Unknown exception occured" << endl;
        }

        cout << TradeVector.size() << endl;

}

int main()
{

        classified();

}

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Ben Hutchings
Sent: 18 April 2005 17:04
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] RE: Boost thread problem

Sliwa, Przemyslaw (London) wrote:
> Wow, wow, wow,
>
> Guys, this was my private program. I simply did not have access to the
private email account from work.
> As you have probably seen the issue was not with the CFXTrade class
but rather with the vectro<T> one.

I very much doubt that there's a bug in the std::vector implementation
you're using. More likely CFXTrade doesn't meet the requirements for
vector element types (CopyConstructible and Assignable). But and queue
uses deque by default, and deque imposes the same requirements, so even
if your program works on your current compiler it could break if you use

any other compiler, or a different version, or even different options.
Of course, we can't really tell if you don't post the missing code.

Ben.
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
--------------------------------------------------------

If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail. http://www.ml.com/email_terms/
--------------------------------------------------------


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