The only guarantees made in regards to alignment are the ones outlined here:
https://golang.org/ref/spec#Size_and_alignment_guarantees
>From what I can tell, there is no such guarantee. But why do you care,
specifically? Using the sync/atomic package will handle this correctly in
every case. This reeks of an XY-problem <http://xyproblem.info/> - what is
the *actual problem* you are trying to solve? There likely is a better way
to handle your problem (e.g. you say that there is only a single write, so
sync.Once <https://godoc.org/sync#Once> might help? Or sync.Cond
<https://godoc.org/sync#Cond>?). When I said your program will be
incorrect, I wasn't being cautious, I was being literal.

On Mon, Jan 9, 2017 at 3:03 PM, 陈诚 <ncu.br...@gmail.com> wrote:

> Thanks for your caution. Then is there a way to know that a certain
> variable is aligned properly? Will the compiler make the global variable
> `var p *int` in my sample code aligned properly?
>
> 在 2017年1月9日星期一 UTC+8上午3:17:37,Dave Cheney写道:
>
>> What you are talking about is called a torn write, which can occur if a
>> value is written to memory but not aligned properly as the processor or
>> memory subsystem must convert this write into two to correct for the miss
>> alignment.
>>
>> Most processors that I know of, and all the ones that Go supports,
>> assuming that the value is correctly alligned will write the value
>> atomically, IE another processor will not see a partially written value.
>>
>> However, I must caution you that while you say it is ok for one processor
>> to see an old value for a time, this is not how the Go memory model works.
>> There are no concessions for "for a time" and so on, the updated value may
>> never be written to memory, or the old value may continue to be visible for
>> the remainder of the program's run time.
>>
>> The memory model describes what you ask for as a data race and states
>> that your program is no longer guaranteed to run correctly. Or put more
>> suscinctly, if you have a data race, the result of your program is
>> undefined.
>>
>> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to