I have two classes InstrumentReader which reads from a file and populates a vector of string called SingleLineVector_. This vector is to be consumed inside InstrumentProcessor via the InstrumentManager, where InstrumentManager Class contains objects of InstrumentReader and InstrumentProcessor.
Each line in this file consists of space delimited index and 25 data points, for example,
1 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 2.22 2.23 2.24 2.25
where the 1st data point is an index and the rest can be considered as 25 Instruments(objects of class InstrumentProcessor).
The InstrumentProcessor class looks like below.
#ifdef INSTRUMENTPROCESSOR_H
#define INSTRUMENTPROCESSOR_H
#include <vector>
#include <thread>
class InstrumentProcessor
{
public:
InstrumentProcessor(int NUM_THREADS);
~InstrumentProcessor();
bool PopulateData(double& data);
bool PrintData();
private:
bool getMeanAndStandardDeviation();
bool CleanData();
private:
double data_;
int N_;
double Mean_;
double M2_;
double stdDev_;
double InitialValue_;
bool Init_;
};
#endif /* INSTRUMENTPROCESSOR_H */
The data_ member variable corresponds to the decimal values in the line above, for example, InstrumentProcessor[0].data_ = 2.1 (Please disregard the private classifier here, this is just to explain my point.)
Now my idea is to multithread the InstrumentProcessor::CleanData() method such that each thread processes 5 InstrumentProcessor objects. What i mean to say is that i will spawn five threads and each thread should process 5 such InstrumentProcessor CleanData() methods, in a way creating a vector of threads.
I am not able to get around the way to define this thread vector which can later be joined.
Please find the InstrumentManager Class definition below.
#ifdef INSTRUMENTMANAGER_H
#define INSTRUMENTMANAGER_H
#include "InstrumentReader.h"
#include "InstrumentProcessor.h"
#include <thread>
#include <vector>
#include <string>
#include <mutex>
#include <boost/algorithm/string.hpp>
class InstrumentManager
{
public:
InstrumentManager(std::string& filename);
~InstrumentManager();
bool ProcessInstruments();
private:
InstrumentReader Reader_;
std::string SingleLine_;
std::vector<InstrumentProcessor> InstrumentProcessorVector_;
std::vector<std::thread> ProcesorThreads_;
std::vector<std::string> VectorToProcess_;
int NUM_PROCESSING_THREADS;
mutable std::mutex InstrumentManagerMutex_;
};
#endif /* INSTRUMENTMANAGER_ */
The InstrumentReader class can be found here:
#ifndef INSTRUMENTREADER_H
#define INSTRUMENTREADER_H
#include <cstdlib>
#include <fstream>
#include <string>
#include <thread>
class InstrumentReader
{
public:
InstrumentReader(std::string& filename);
~InstrumentReader();
std::vector<std::string> getSingleLineVector() { return SingleLineVector_; }
private:
bool ReadFileAndPopulateChunk();
private:
std::string filename_;
const int instSize_;
std::vector<std::string> SingleLineVector_;
boost::mutex ReaderMutex_;
};
#endif /* INSTRUMENTREADER_H */
Any help on this will be highly appreciated.
Aucun commentaire:
Enregistrer un commentaire