最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Is it safe to copy a constant global std::exception_ptr wo locking? - Stack Overflow

programmeradmin3浏览0评论

My understanding is that the general rule for types in the C++ standard library that using them concurrently w/o locking is fine as long as all the concurrent operations are reads

I 'know' that internally, std::exception_ptr is implemented via some sort of reference counting, so copying it actually involves some sort of write(s), is that an issue for code like the following?

// Global scope
std::exception_ptr GetSharedException() noexcept
{
    try
    {
        static const std::exception_ptr result(std::make_exception_ptr(CreateException()))
        return result;
    }
    catch (...)
    {
        return std::current_exception();
    }
}

// Some function which can be running on multiple threads concurrently
void foo() noexcept
{
    // Do stuff
    if (bar())
        consume(GetSharedException())
    // Do more stuff
}
发布评论

评论列表(0)

  1. 暂无评论