c++ - 当写在不同的索引上时,这个矩阵类线程安全吗?

标签 c++ multithreading visual-c++ boost

您可能会注意到这背后有一个双 * 存储空间。任何读取/写入的值都被转换为平面索引,因此它存储在一个大的单维数组中。

我需要知道我是否可以在不同线程中写入此类对象的不同索引?我正在使用运算符 () 来设置值。边界是在实例化对象时预定义的。即:矩阵 m(rows,cols); 我在 boost 线程中使用它并遇到问题。我需要放置任何作用域锁吗?但我相信这将是一种性能成本。

class matrix
{
public:
    matrix(size_t rows, size_t cols):_rows(rows),_cols(cols),_data(0)
    {
        reset(rows,  cols);
    }
    matrix():_data(0),_rows(0),_cols(0){}
    ~matrix()
    {
        if (0 != _data)
        {
            delete [] _data;
        }
    }

    matrix(const matrix& copythis)
        :_data(0),_rows(0),_cols(0)
    {
        if (0 != _data)
        {
            delete [] _data;
        }
        _rows = copythis.rows();
        _cols = copythis.cols();
        _data = new double [_rows * _cols];

        if (0 == _data)
        {
            NL_THROW("Insufficient memory to create a cloned matrix of double of " << _rows << " X " << _cols);
        }

        memcpy(_data, copythis._data, _rows * _cols * sizeof(double) );

    }


public:

    const matrix& operator = (const matrix& copythis)
    {
        if (0 != _data)
        {
            delete [] _data;
        }
        _rows = copythis.rows();
        _cols = copythis.cols();
        _data = new double [_rows * _cols];

        if (0 == _data)
        {
            NL_THROW("Insufficient memory to create a cloned matrix of double of " << _rows << " X " << _cols);
        }

        memcpy(_data, copythis._data, _rows * _cols * sizeof(double) );

        return (*this);

    }







    double* cArray()
    {
        if (0 == _data)
        {
            NL_THROW("Matrix is not initialised");
        }
        return _data;
    }



    void reset(size_t rows, size_t cols)
    {
        if (0 != _data)
        {
            delete [] _data;
        }
        _rows = rows;
        _cols = cols;
        _data = new double[rows * cols];

        if (0 == _data)
        {
            NL_THROW("Insufficient memory to create a matrix of double of " << _rows << " X " << _cols);
        }
        memset(_data, 0, sizeof(double) * _rows * _cols);
    }





    double& operator () (size_t rowIndex, size_t colIndex) const
    {
        if (rowIndex >= _rows)
        {
            NL_THROW("Row index " << rowIndex << " out of range(" << _rows - 1 << ")");
        }

        if (colIndex >= _cols)
        {
            NL_THROW("Column index " << colIndex << " out of range(" << _cols - 1 << ")");
        }

        size_t flatIndex = colIndex + rowIndex * _cols;
        return _data[flatIndex];

    }


    Array rowSlice(size_t rowIndex) const
    {
        if (rowIndex >= _rows)
        {
            NL_THROW("Cannot slice matrix, required row: " << rowIndex << " is out of range(" << (_rows - 1) << ")");
        }
        Array retval(_data + rowIndex * _cols, _cols);

        /*
        for(size_t i = 0; i < _cols; i++)
        {
            retval[i] = operator()(rowIndex, i);
        }
        */
        return retval;
    }



    Array colSlice(size_t colIndex) const
    {
        if (colIndex >= _cols)
        {
            NL_THROW("Cannot slice matrix, required row: " << colIndex << " is out of range(" << (_cols - 1) << ")");
        }
        Array retval(_rows);
        for(size_t i = 0; i < _rows; i++)
        {
            retval[i] = operator()(i, colIndex);
        }
        return retval;
    }


    void fill(double value)
    {
        for(size_t rowIndex = 0; rowIndex < _rows; rowIndex++)
        {
            for(size_t colIndex = 0; colIndex < _cols; colIndex++)
            {
                size_t flatIndex = colIndex + rowIndex * _cols;
                _data[flatIndex] = value;
            }
        }
    }


    bool isEmpty() const
    {
        if (0 == _rows) return true;
        if (0 == _cols) return true;
        return false;
    }

    size_t rows() const {return _rows;}
    size_t cols() const {return _cols;}

private:
    double* _data;
    size_t _rows;
    size_t _cols;
};

最佳答案

如果您可以保证您的所有线程永远不会 在矩阵的同一元素上发生冲突,那么您就不需要锁定。实际上,期望这样的保证是不现实的,因此您需要锁定。

关于c++ - 当写在不同的索引上时,这个矩阵类线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25002641/

相关文章:

c++ - 如何处理类定义的数组数组

c++ - 根据intel博客实现concurrent_vector

c++ - 为什么在 VCC 2003 中编译需要这么长时间?

c++ - OF_SHARE_DENY_NONE 不分享阅读?

c++ - 为什么 stoi 函数在 Visual Studio 2010 中可用

c++ - GCC - 类型声明的确切位置

python - 如何在 main 仍在 python 中运行时使用线程实时获取用户输入

multithreading - Swift iOS9 - dispatch_group - 需要代码以特定顺序执行

c++ - Compaq Visual Fortran 中对 DFOR.LIB 的引用

c++ - 从 USBPcap 库读取原始数据