Thanks. So I take it this must be handled by the compiler when generating 
assembly.

I have peeked in the src/cmd/compile/ directory , assuming it is hidden 
there, but that's a rabbit hole I do want to go in just yet.

What I am really looking for is knowing which instructions lead to 
memory-synchronisation. I assume there are  'Synchronisation primitives' 
which have this property and therefore other primitives have not.

E.g. A co-worker pointed out that you can avoid sync.Mutex by using this 
construct:

        if !atomic.CompareAndSwapInt32(&s.myLock, 0, 1) {
                fmt.Println("locked")
                return
        }
        defer atomic.StoreInt32(&s.myLock, 0)
        processData()

Would this synchronise memory?

On Monday, June 15, 2020 at 6:42:16 PM UTC+2 bbse...@gmail.com wrote:

> On Mon, Jun 15, 2020 at 10:30 AM Leo Baltus <leo.b...@npo.nl> wrote:
> >
> > from gopl chapter 9.4 'Memory synchronisation’
> >
> > Synchronization primitives like channel communications and mutex 
> operations cause the processor to flush out and commit all its accumulated 
> writes so that the effects of goroutine execution up to that point are 
> guaranteed to be visible to goroutines running on other processors.
> >
> > I would like to better understand how this works. What is it that makes 
> this ‘flush out’ to happen? Is this a system call?
>
> It is usually done with a memory barrier:
>
> https://en.wikipedia.org/wiki/Memory_barrier
>
>
> >
> > —
> > Leo
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google 
> Groups "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to golang-nuts...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/F663D146-9F4C-48BB-918A-E58D554B9B18%40npo.nl
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/18a4052a-4683-46f8-96c7-cda92a34a875n%40googlegroups.com.

Reply via email to