I am initializing a vector in a class using 4 threads by passing arguments to threads:
#include "stdafx.h"
#include <stdlib.h>
#include <vector>
#include "class.h"
using namespace std;
int main()
{
myCLASS* classObject = new myCLASS();
classObject->classMemberFunction();
return 0;
}
and
#include "stdafx.h"
#include <vector>
#include <mutex>
#ifndef SLIC_H
#define SLIC_H
class myCLASS
{
protected:
std::vector<int> cluster;
std::mutex cluster_mutex;
int iterations;
void clearData();
public:
myCLASS();
myCLASS(const myCLASS& othermyCLASS);
virtual ~myCLASS();
void classMemberFunction();
void thread2(std::vector<int> *cluster, int *iterations);
void thread3(std::vector<int> *cluster, int *iterations);
void thread4(std::vector<int> *cluster, int *iterations);
};
#endif
and
#include "stdafx.h"
#include "class.h"
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
using namespace std;
myCLASS::myCLASS()
{
clearData();
}
myCLASS::myCLASS(const myCLASS& othermyCLASS)
{
}
myCLASS::~myCLASS()
{
clearData();
}
void myCLASS::clearData()
{
this->cluster.clear();
}
void myCLASS::classMemberFunction(void)
{
this->iterations = 1000;
thread t2(&myCLASS::thread2, *this, &cluster, &iterations);
thread t3(&myCLASS::thread3, *this, &cluster, &iterations);
thread t4(&myCLASS::thread4, *this, &cluster, &iterations);
for (int n = 0; n < iterations / 4; ++n)
{
cluster.push_back(-1);
}
t2.join();
t3.join();
t4.join();
cout << cluster.size() << endl;
system("PAUSE");
}
void myCLASS::thread2(std::vector<int> *cluster, int *iterations)
{
cluster_mutex.lock();
for (int n = 0; n < (*iterations) / 4; ++n)
{
cluster->push_back(-1);
}
cluster_mutex.unlock();
}
void myCLASS::thread3(std::vector<int> *cluster, int *iterations)
{
cluster_mutex.lock();
for (int n = 0; n < (*iterations) / 4; ++n)
{
cluster->push_back(-1);
}
cluster_mutex.unlock();
}
void myCLASS::thread4(std::vector<int> *cluster, int *iterations)
{
cluster_mutex.lock();
for (int n = 0; n < (*iterations) / 4; ++n)
{
cluster->push_back(-1);
}
cluster_mutex.unlock();
}
the thing is I don't want to pass those two arguments to threads. I want threads to explicitly access data members
iterations
and
cluster
and manipulate them by taking `void` as arguments !
is there any paralleling done with this code at all ?