如何将mutex作为类成员?解决simple_encoder拷贝构造函数隐式删除问题
Hey, I get exactly where you're stuck here—let's cut straight to the root of the issue: std::mutex is a non-copyable (and non-movable) type. When your simple_encoder class has a std::mutex as a member, the C++ compiler automatically deletes your class's copy constructor and copy assignment operator behind the scenes. Why? Because it can't copy the mutex (and copying a lock makes zero sense for thread safety anyway), so the default copy logic that tries to copy every member falls apart.
Looking at your code snippet:
namespace master_thesis {
class simple_encoder ...
Here are the most common fixes, depending on what you need your encoder to do:
1. Explicitly disable copying (the most straightforward fix)
If you never need to copy simple_encoder objects in the first place, just explicitly delete the copy constructor and assignment operator. This makes the compiler error way clearer, and prevents accidental copying down the line:
namespace master_thesis { class simple_encoder { private: std::mutex m_mutex; // Your other members here... public: // Kill the copy operations explicitly simple_encoder(const simple_encoder&) = delete; simple_encoder& operator=(const simple_encoder&) = delete; // Your other constructors/methods go here simple_encoder() = default; // Example default constructor }; }
Now if you accidentally try to copy an encoder later, the compiler will yell at you with a direct message instead of that vague "implicitly deleted" error.
2. Use a smart pointer for the mutex (if you need shared locking)
If you need multiple simple_encoder instances to share the same mutex (like for coordinating access to a shared resource), wrap the mutex in a std::shared_ptr:
namespace master_thesis { class simple_encoder { private: std::shared_ptr<std::mutex> m_mutex; // Other members... public: simple_encoder() : m_mutex(std::make_shared<std::mutex>()) {} // Now copy operations work automatically because shared_ptr is copyable // Copied encoders will share the same underlying mutex }; }
Just be careful with this—make sure sharing the same lock is actually what you want for your thread safety logic.
3. Fix accidental copying in your code
Double-check where you might be accidentally copying your simple_encoder objects. Common culprits include:
- Passing the encoder by value to a function (use a reference or pointer instead)
- Storing encoders in containers that trigger copies (like some
std::vectoroperations) - Any other place where the compiler might try to make a copy without you noticing
For example, if you had a function like this:
void process_encoder(simple_encoder encoder) { /* ... */ }
Change it to pass by reference:
void process_encoder(simple_encoder& encoder) { /* ... */ }
Quick side note
The reason std::mutex can't be copied is intentional—copying a lock doesn't copy its locked/unlocked state, which would break all your thread synchronization logic. The C++ standard intentionally disables copy and move for mutex types to prevent this kind of bug.
Most of the time, the first fix (disabling copies) is what you need. Just make sure your code doesn't try to copy the encoder, and you'll be good to go.
内容的提问来源于stack exchange,提问作者Lars Kakavandi-Nielsen

