在不同的索引上写这个矩阵类线程是否安全?

时间:2014-07-28 19:16:40

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

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

我需要知道我是否可以在不同的线程中写入此类对象中的不同索引?我正在使用operator()来设置值。在对对象进行实例化时预定义边界。即:矩阵m(行,列); 我在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;
};

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

编写此类的方式,除了getter rows()cols()之外,您需要锁定每个方法,几乎​​在每个方法的最广泛范围内

你甚至不能依赖size_t类型的原子来保存任何锁,因为reset()非原子地设置了_row_col。任何需要了解_row_col的操作,例如isEmpty(),如果从另一个线程调用reset(),则会遇到麻烦。