1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

copy constructor in class with std::mutex

Discussion in 'Programming/Internet' started by cerr, Oct 8, 2018.

  1. cerr

    cerr Guest

    This question was migrated from Stackoverflow.com as it asks for review. It compiles and runs but I'm wondering if what I'm doing is correct and if it should be improved/fixed:

    class A {
    public:
    A(){}
    ~A(){}
    };
    class my_t {
    private:
    bool busy;
    std::mutex amtx;
    public:
    my_t() {std::lock_guard<std::mutex>lock(amtx);busy=false;}
    ~my_t(){}
    my_t(const my_t &a) {amtx.lock();conn = a.conn;amtx.unlock();bmtx.lock();busy = a.busy;bmtx.unlock();} //copy constructor
    A* conn;

    };


    I need the copy constructor to be able to push multiple instances of this class onto a vector.

    Once on the vector, they will be given to multiple clients as required. There will be an access function to read/write to busy which will control the usage of each instance.
    A *conn will be monitored by an asynchronous watchdog thread (hence usage of separate mutex).

    Login To add answer/comment
     

Share This Page