Skip to content

Instantly share code, notes, and snippets.

@andr1972
Last active January 29, 2018 01:31
Show Gist options
  • Save andr1972/f63cf132abef474dc9e25491d3115351 to your computer and use it in GitHub Desktop.
Save andr1972/f63cf132abef474dc9e25491d3115351 to your computer and use it in GitHub Desktop.
Ask how to share thread data
#include "Finder.h"
#include <algorithm>
#include <random>
#include <chrono>
#include <thread>
#include <string>
FinderForTx::FinderForTx()
{
}
FinderForTx::~FinderForTx()
{
}
OutputTx FinderForTx::findPred(InputTx &inputVec)
{
default_random_engine dre(random_device{}());
uniform_int_distribution<int> id(1, 20);
this_thread::sleep_for(chrono::milliseconds(id(dre)));
OutputTx output;
output.strNumber = to_string(inputVec.number);
return output;
}
void FinderForBlock::execute(vector<InputTx> *inputVec, vector<OutputTx> *outputVec)
{
FinderForTx finder;
for (size_t i = 0; i<inputVec->size(); i++)
{
outputVec->at(i) = finder.findPred(inputVec->at(i));
}
}
vector<OutputTx> FinderForBlock::findPred(vector<InputTx> &inputVec)
{
vector<OutputTx> outputVec;
outputVec.resize(inputVec.size());
if (nThreads == 0)
{
FinderForTx finder;
for (size_t i = 0; i<inputVec.size(); i++)
{
outputVec[i] = finder.findPred(inputVec[i]);
}
}
else
{
vector<thread> vec_thr;
for (int i=0; i<nThreads; i++)
vec_thr.emplace_back(&execute, &inputVec, &outputVec);
for (auto& t : vec_thr)
t.join();
}
return outputVec;
}
/*see:
http://www.cplusplus.com/forum/unices/194352/ - vector of threads, no mutex example
http://deathbytape.com/articles/2015/02/03/cpp-threading.html - lock_guard<mutex>, main thread + one thread, mutex is global
*/
#pragma once
#include <vector>
using namespace std;
struct InputTx
{
int number;
};
struct OutputTx
{
string strNumber;
};
class FinderForTx
{
public:
FinderForTx();
virtual ~FinderForTx();
OutputTx findPred(InputTx &inputVec);
};
class FinderForBlock
{
int nThreads;
static void execute(vector<InputTx> *inputVec, vector<OutputTx> *outputVec);
public:
FinderForBlock(int nThreads) :nThreads(nThreads) {};
virtual ~FinderForBlock(){};
vector<OutputTx> findPred(vector<InputTx> &inputVec);
};
#include "Finder.h"
#include <random>
#include <string>
void gen(vector<InputTx> &inputVec)
{
default_random_engine dre(0);
uniform_int_distribution<int> id(1, 100);
for (int i=0; i<100; i++)
{
InputTx input;
input.number = id(dre);
inputVec.push_back(input);
}
}
void check(vector<InputTx> &inputVec, vector<OutputTx> &outputVec)
{
if (inputVec.size() != outputVec.size())
{
printf("sizes mismatch!\n");
return;
}
for (size_t i=0; i<inputVec.size(); i++)
if (to_string(inputVec[i].number)!= outputVec[i].strNumber)
{
printf("number[%d] %d changed to [%s]\n", i,inputVec[i].number, outputVec[i].strNumber.c_str());
return;
}
}
int main()
{
vector<InputTx> inputVec;
gen(inputVec);
FinderForBlock finder(2);
vector<OutputTx> outputVec = finder.findPred(inputVec);
check(inputVec, outputVec);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment