Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am initializing a vector in a class using 4 threads by passing arguments to threads:

C++
//main.cpp
    #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

C++
//class.h
    #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();
        /* Class copy constructor. */
        myCLASS(const myCLASS& othermyCLASS);

        /* Class destructor. */
        virtual ~myCLASS();

        void classMemberFunction();
        //creating threads function
        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

C++
//class.cpp
    #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()
    {
        /* Erase all matrices' elements. */
        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);

        //main thread===================================================
        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 ?
Posted
Comments
barneyman 16-Apr-15 21:18pm    
your cluster_mutex is killing any parallelism

those thread functions are members of the class, they have explicit access to the vector/int already - you're already calling the mutex

i'm not sure i understand your problem/required solution

1 solution

You can declare some data structure (struct) which holds all of your data. That can be set at thread start or as global data.
It is a common practice to use pointers to that data.

C++
struct DATA
{
  std::vector<int> *cluster;
  int* iteration;
};

//possible array for 4 threads
DATA threadData[4];
</int>


Be sure to write enough check code for valid access.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900