My application has an IRC module, which is essentially a regular client. Since this is very upside down, I risk getting a plug-in, for example, a user alias - it is valid at that time, but the parser starts the update by changing the specified alias. After another thread executes again, it deals with a pointer to now invalid memory, since it will be impossible to return return + as an atomic operation.
I assume this is correct, based on the code below? As such, I assume that I will have to use the usual mutex lock / unlock method if someone cannot confirm or suggest another (I would prefer not to convert and return shared_ptr, but I think this is a valid option, it's just me I intend on SWIG'ing this and donβt know if they will like it).
IrcUser.h
class IrcUser : public IrcSubject { private: ... std::shared_ptr<std::string> _nickname; std::shared_ptr<std::string> _ident; std::shared_ptr<std::string> _hostmask; public: ... const c8* Ident() const { return _ident.get()->c_str(); } const c8* Hostmask() const { return _hostmask.get()->c_str(); } const u16 Modes() const { return _modes; } const c8* Nickname() const { return _nickname.get()->c_str(); } bool Update( const c8 *new_nickname, const c8 *new_ident, const c8 *new_hostmask, const mode_update *new_modes ); };
IrcUser.cc
bool IrcUser::Update( const c8 *new_nickname, const c8 *new_ident, const c8 *new_hostmask, const mode_update *new_modes ) { if ( new_nickname != nullptr ) { if ( _nickname == nullptr ) { *_nickname = std::string(new_nickname); } else { _nickname.reset(); *_nickname = std::string(new_nickname); } Notify(SN_NicknameChange, new_nickname); } ... }
thread-safety c ++ 11 unique-ptr shared-ptr
ZXcvbnM
source share