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
#include
#include
#include
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 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; ijoin();
}
out.close();
for(int i=0; i ReadThreadVector(number_of_threads);
in.open("file.txt");
try
{
for(int i=0; ijoin();
}
in.close();
for(int i=0; ihttp://www.ml.com/email_terms/
--------------------------------------------------------