Re: Does object constructor force a memroy barrier in C++?

101 views
Skip to first unread message

Dmitry Vyukov

unread,
Apr 14, 2018, 2:23:03 AM4/14/18
to Huapeng Yuan, lock...@googlegroups.com
On Fri, Apr 13, 2018 at 12:02 AM, Huapeng Yuan <huape...@gmail.com> wrote:
> Hi Mr. Dmitriy,
> I contacted you few years back. I know you are an expert in concurrency
> programming. I have a question about object constructor and memory barrier.
>
> Let's say we have a calss ABC,
> class ABC {
> ABC() {
> lock_.Lock()
> map_.emplace_back("key", "value");
> lock_.Unlock()
> }
>
> private:
> unordered_map map_;
> Lock lock_;
> };
>
> I am wondering whether the lock_ is necessary to force a barrier in the
> constructor so that other threads can see the latest value in the map? A
> general question is whether object constructor force a memroy barrier?

+lock-free mailing list

Hi Yuan,

No, constructors in C++ do not have any memory barriers.
However, when a constructor starts executing the object is not
published yet (not accessible to other threads). And it's the object
publication mechanism that needs to ensure that object contents are
properly visible to other threads.
Reply all
Reply to author
Forward
0 new messages