如何访问线程中的类数据成员而不将它们作为参数传递? [英] How to access class data members in a thread without passing them as arguments?

查看:128
本文介绍了如何访问线程中的类数据成员而不将它们作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用4个线程初始化类中的向量,方法是将参数传递给线程:



I am initializing a vector in a class using 4 threads by passing arguments to threads:

//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

//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

//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();
    }





事情是我不想将这两个参数传递给线程。我希望线程显式访问数据成员 iterations cluster 并通过将`void`作为参数来操作它们。 />


是否有任何与此代码完全并行的工作?



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 ?

推荐答案

您可以声明一些数据结构(struct )它包含您的所有数据。这可以在线程启动时设置或作为全局数据设置。

使用指向该数据的指针是常见的做法。



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.

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.


这篇关于如何访问线程中的类数据成员而不将它们作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆