On 25 Feb 2015 18:09, "enormouspenguin" <kimkh...@gmail.com> wrote:
>
> Hi
>
> By "atomic data types" I means that variables of those types could be read/written atomically without the need of importing sync/atomic package. Also, how to atomically read/write a variable of type channel?
>
There are none.
All data types require additional syncronisation.
Use a lock to safely write to a variable of any type.
Have you looked at the sync/atomic package ?
--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.For more options, visit https://groups.google.com/d/optout.
That would work too provided closed isn't ever set back to false elsewhere in a more realistic program
Seems ok... but if you use sync.Once, then you have the further guarantee that after any call to close() returns, that it has completed execution exactly once. That is to say, unlike your code, on the second close, it won't just early return. It will wait until the first close() completes.
Could you describe your concern in code, not words ?
package main
import ( "fmt" "sync/atomic")
var closed uint32
func close() { //Could be concurrently called by many goroutines if atomic.CompareAndSwapUint32(&closed, 0, 1) { //Do some cleanup fmt.Println("Closed successfully!") //If implement correctly, //this line should only be printed once }}
func main() { go close() close()}package main
import ( "fmt" "sync/atomic")
var closed uint32
func close() { //Could be concurrently called by many goroutines
//(*) //Do some cleanup fmt.Println("Closed successfully!") //If implement correctly, //this line should only be printed once //end of (*)
if atomic.CompareAndSwapUint32(&closed, 0, 1) { //(*) should be here, but instead it's hoisted above by compiler or CPU (just my theory) }}
func main() { go close() close()}Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program. That is, compilers and processors may reorder the reads and writes executed within a single goroutine only when the reordering does not change the behavior within that goroutine as defined by the language specification.
I think the first two sentences in the "Happens Before" section answer your question:Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program. That is, compilers and processors may reorder the reads and writes executed within a single goroutine only when the reordering does not change the behavior within that goroutine as defined by the language specification.
Do you mean just by calling atomic.CompareAndSwapUint32(&closed, 0, 1), I could actually issue a memory fence (barrier) instruction to CPU (even without the present of IF (branching) statement), therefore preventing reads/writes from reordering around it?
Ø Do you mean just by calling atomic.CompareAndSwapUint32(&closed, 0, 1), I could actually issue a memory fence (barrier) instruction to CPU (even without the present of IF (branching) statement), therefore preventing reads/writes from reordering around it?
It is my understanding that this is so, and it is so for all of the primitives in sync/atomic. Check this thread from a couple of weeks ago:
Compiler level memory barrier in Go i.e. asm volatile("" : : : "memory")
John
John Souvestre - New Orleans LA
My constrain is that the code inside the IF must run only once, no matter how many times the function was called and how many goroutines the function was concurrently called from.
I don't think it is sufficient to just use separate Load and Store in my case. What if 2 or more goroutines all receive the same value before one of those have a chance to change it? My constrain is that the code inside the IF must run only once, no matter how many times the function was called and how many goroutines the function was concurrently called from. If that scenario happen, the code would be called multiple times, hence violating the constrain. Am I missing something here?