简体   繁体   English

C++中的线程安全数据结构

[英]Thread safe data structure in C++

Like to make a thread safe C++ data structure as follows.喜欢做一个线程安全的C++数据结构如下。

struct infos{
  int crowdinfos[10][horGridNums*verGridNums];
  int cameraSourceID;
  static int idx;
  std::mutex mutex;
  
};

mutex.lock and unlock will be used to be thread safe. mutex.lock 和 unlock 将用于线程安全。

EDIT: In my header file, I'll have a vector of infos.编辑:在我的 header 文件中,我将有一个信息向量。

std::vector<infos> c_infos;

When I build with g++, I have errors as当我使用 g++ 构建时,出现以下错误

/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘infos::infos(const infos&)’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from gstdsexample.cpp:29:0:
gstdsexample.h:66:8: note: ‘infos::infos(const infos&)’ is implicitly deleted because the default definition would be ill-formed:
 struct infos{
        ^~~~~
gstdsexample.h:66:8: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’

The whole log infos in build are as follows.构建中的整个日志信息如下。

-fPIC -DDS_VERSION="5.0.0" -I /usr/local/cuda-10.2/include -I ../../includes -pthread -I/usr/include/gstreamer-1.0 -I/usr/include/orc-0.4 -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/opencv4/opencv -I/usr/include/opencv4
g++ -c -o gstdsexample.o -fPIC -DDS_VERSION=\"5.0.0\" -I /usr/local/cuda-10.2/include -I ../../includes -pthread -I/usr/include/gstreamer-1.0 -I/usr/include/orc-0.4 -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/opencv4/opencv -I/usr/include/opencv4 gstdsexample.cpp
In file included from /usr/include/c++/7/bits/stl_tempbuf.h:60:0,
                 from /usr/include/c++/7/bits/stl_algo.h:62,
                 from /usr/include/c++/7/algorithm:62,
                 from /usr/include/opencv4/opencv2/core/base.hpp:55,
                 from /usr/include/opencv4/opencv2/core.hpp:54,
                 from /usr/include/opencv4/opencv2/imgproc.hpp:46,
                 from /usr/include/opencv4/opencv2/imgproc/imgproc.hpp:48,
                 from gstdsexample.h:30,
                 from gstdsexample.cpp:29:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = infos; _Args = {const infos&}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:83:18:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const infos*, std::vector<infos> >; _ForwardIterator = infos*; bool _TrivialValueTypes = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:134:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const infos*, std::vector<infos> >; _ForwardIterator = infos*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const infos*, std::vector<infos> >; _ForwardIterator = infos*; _Tp = infos]’
/usr/include/c++/7/bits/stl_vector.h:331:31:   required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = infos; _Alloc = std::allocator<infos>]’
gstdsexample.cpp:444:40:   required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘infos::infos(const infos&)’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from gstdsexample.cpp:29:0:
gstdsexample.h:66:8: note: ‘infos::infos(const infos&)’ is implicitly deleted because the default definition would be ill-formed:
 struct infos{
        ^~~~~
gstdsexample.h:66:8: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from /usr/include/c++/7/mutex:43:0,
                 from /usr/include/opencv4/opencv2/core/utility.hpp:62,
                 from /usr/include/opencv4/opencv2/core.hpp:3291,
                 from /usr/include/opencv4/opencv2/imgproc.hpp:46,
                 from /usr/include/opencv4/opencv2/imgproc/imgproc.hpp:48,
                 from gstdsexample.h:30,
                 from gstdsexample.cpp:29:
/usr/include/c++/7/bits/std_mutex.h:97:5: note: declared here
     mutex(const mutex&) = delete;

What could be the correct way of using inside Struct?在 Struct 中使用的正确方法是什么?

As you haven't posted your code entirely, I'm not sure what your problem is.由于您尚未完全发布代码,因此我不确定您的问题是什么。 So please look at my example below instead.所以请看我下面的例子。

Here is how I use a mutex for a queue.这是我如何将互斥锁用于队列。 By looking at it you can adapt your struct for whatever it is you are trying to accomplish.通过查看它,您可以调整您的结构以适应您想要完成的任何事情。

class Queue {
public:
    Queue() = default;

    /**
     * Push a message to the queue.
     */
    void push(const std::array<int, 4>& message) {
        while (true) {
            std::unique_lock<std::mutex> locker(mu);
            cond.wait(locker, [this](){ return buffer_.size() < size_; });
            buffer_.push_back(message);
            locker.unlock();
            cond.notify_all();
            return;
        }
    }

    /**
     * Pop a message off the queue.
     */
    bool pop(std::array<int, 4>& value) {
        while (true) {
            std::unique_lock<std::mutex> locker(mu);
            if (buffer_.size() == 0) {
                return false;
            }
            value = buffer_.front();
            buffer_.pop_front();
            locker.unlock();
            cond.notify_all();
            return true;
        }
    }

private:
    std::mutex mu;
    std::condition_variable cond;

    std::deque<std::array<int, 4>> buffer_;
    const unsigned int size_ = 200;
    
};

In a nutshell, wherever you want thread safe access to some object do something like:简而言之,无论您希望线程安全地访问某些 object,请执行以下操作:

std::unique_lock<std::mutex> locker(mu);
            
//Add Your code on the object you want thread safe access to.

            locker.unlock();
            cond.notify_all();

The important thing is, that you only access the object you want thread safe access to within the locker / unlock as above, and that you notify anyone waiting on the object as described above.重要的是,您只能访问 object,您希望线程安全地访问锁柜/解锁,如上所述,并且您通知任何在 object 上等待的人。

Please see your error message:请查看您的错误消息:

‘infos::infos(const infos&)’ is implicitly deleted because the default definition would be ill-formed:  struct infos{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM