Boost logo

Boost Users :

From: Sliwa, Przemyslaw (London) (Przemyslaw_Sliwa_at_[hidden])
Date: 2005-04-13 12:07:02


All,
 
I have got a problem with the boost::thread package.
My idea was to facilitate the multiprocessor ability of my computer and
write a program which creates 10 threads which firstly write to the disk
certain amount of information and then they read it back and push back
certain objects. I am getting some memory access problems when pushing
back the objects to the global vector. Can anyone of you understand why
there is a problem? I really do not get it since the mutex is locked and
the access to the vector is exclusively guaranteed to the active thread.

 
Can you help?
 
Thank you in advance
 
Pshemek
 
#include "FXTrade.h"

#include <string>

#include <fstream>

#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(8);

}

vector<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(vector<CFXTrade>& nTradeVector)

{

using namespace write_namespace;

using namespace read_namespace;

string c_tmp;

try

{

for(size_t i=0; i<dimension; ++i)

{

mutex::scoped_lock lock(in_file_mutex);

getline(in, c_tmp);

TradeVector.push_back(*getTradeFromString(c_tmp) );

}

}

catch(exception& e)

{

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();

}

out.close();

for(int i=0; i<number_of_threads; ++i)

{

delete ThreadVector[i];

}

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();

}

in.close();

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;

}

return true;

}

int main()

{

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;

}
--------------------------------------------------------

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