3

我的应用程序有一个 IRC 模块,它本质上是一个普通的客户端。由于这是大量线程,我承担插件检索例如用户昵称的风险 - 它当时有效,但解析器触发更新,更改所述昵称。一旦另一个线程再次执行,它就会处理指向现在无效内存的指针,因为不可能将返回 + 复制作为原子操作。

根据下面的代码,我对此的假设是否正确?因此,我想我必须使用通常的互斥锁/解锁方法,除非有人可以确认或提出其他建议(我宁愿不必转换并返回 shared_ptr,但我想这是一个有效的选项,它是只是我打算 SWIG'ing 这个,不知道它是否不喜欢它们)。

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);
    }

    ...
}
4

3 回答 3

8

我建议锁定如此细粒度的级别可能(方式)过大。

我建议对 IrcUser 对象本身进行原子更新,这可能是无锁的,具体取决于您的库实现和目标架构。这是一个使用的示例

  • std::atomic_is_lock_free<std::shared_ptr>
  • std::atomic_load<std::shared_ptr>
  • std::atomic_store<std::shared_ptr>

有关文档,请参阅http://en.cppreference.com/w/cpp/memory/shared_ptr/atomic

免责声明 我不知道有多少编译器/C++ 库实现已经实现了这个 C++11 特性。

这是它的样子:

#include <atomic>
#include <memory>
#include <string>

struct IrcSubject {};
typedef char c8;
typedef uint16_t u16;
typedef u16 mode_update;

class IrcUser : public IrcSubject
{
    private:
        // ...
        std::string _nickname;
        std::string _ident;
        std::string _hostmask;
        u16         _modes;
    public:
        IrcUser(std::string nickname, std::string ident, std::string hostmask, u16 modes)
            : _nickname(nickname), _ident(ident), _hostmask(hostmask), _modes(modes) { }
        // ...
        std::string const& Ident()    const { return _ident; }
        std::string const& Hostmask() const { return _hostmask; }
        const u16          Modes()    const { return _modes; }
        std::string const& Nickname() const { return _nickname; }
};

//IrcUser.cc
bool Update(std::shared_ptr<IrcUser>& user,
    std::string new_nickname,
    std::string new_ident,
    std::string new_hostmask,
    const mode_update *new_modes
)
{
    auto new_usr = std::make_shared<IrcUser>(std::move(new_nickname), std::move(new_ident), std::move(new_hostmask), *new_modes /* ??? */);
    std::atomic_store(&user, new_usr);
    //Notify(SN_NicknameChange, new_nickname);
    return true;
}

bool Foo(IrcUser const& user)
{
    // no need for locking, user is thread safe
}

int main()
{
    auto user = std::make_shared<IrcUser>("nick", "ident", "hostmask", 0x1e);

    mode_update no_clue = 0x04;
    Update(user, "Nick", "Ident", "Hostmask", &no_clue);

    {
        auto keepref = std::atomic_load(&user);
        Foo(*keepref);
    }
}
于 2013-05-10T20:21:02.383 回答
4

该代码具有竞争条件,因此具有未定义的行为,因为来自不同线程的同一对象(实例)可能存在读取(the ->get())和写入(the .reset()or ):对s 的访问必须同步。=std::shared_ptr<std::string>std::shared_ptr

注意在 getter 中锁定 astd::mutex并返回c_str()是不够的,因为 getter 的调用者将使用c_str()锁外的结果:getter 需要按值返回 a shared_ptr

纠正:

  • 添加std::mutexIrcUser(注意这现在使类不可复制):

    mutable std::mutex mtx_; // Must be mutable for use within 'const'
    
  • 锁定std::mutexgetter 和Update(),使用 astd::lock_guard以确保异常安全:

    std::shared_ptr<std::string> Nickname() const
    {
        std::lock_guard<std::mutex> l(mtx_);
        return _nickname;
    }
    
    bool IrcUser::Update(const c8 *new_nickname,
                         const c8 *new_ident,
                         const c8 *new_hostmask,
                         const mode_update *new_modes)
    {
        if (new_nickname)
        {
            {
                std::lock_guard<std::mutex> l(mtx_);
                _nickname.reset(new std::string(new_nickname));
            }
            // No reason to hold the lock here.
            Notify(SN_NicknameChange, new_nickname);
        }
    
        return true;
    }
    

std::string如果可以接受复制,请考虑仅使用 a ,因为这shared_ptr可能会增加不必要的复杂性。

于 2013-05-10T20:15:52.187 回答
1

是的,昵称可能会出现竞争条件,导致您的 getter 访问坏内存。shared_ptrs 仅就其所有权语义而言是线程安全的。您将需要为它们的值添加某种形式的同步。

于 2013-05-10T19:54:55.483 回答