First of all: before Go 1.5, it runs on a single processor, using only a few threads to block system calls. If you do not tell at run time to use more processors using GOMAXPROCS .
As of Go 1.5, GOMAXPROCS is set to CPUS. See 6 , 7 .
In addition, the operation *cptr = *cptr + 1 not guaranteed to be atomic. If you look carefully, it can be divided into 3 operations: get the old value using the dereference pointer, increment value, save the value to the pointer address.
The fact that you get 508/510 is due to some magic at runtime and is not defined to stay that way. More information on the behavior of concurrency operations can be found in the Go memory model .
You are probably getting the correct values ββfor <510 running goroutines, because any number below this is not interrupted (for now) interrupted.
As a rule, what you are trying to do is not recommended in any language or in the "Go" method to make concurrency. A very good example of using channels for synchronization is a code walk: Share Memory By Communicating (instead of messaging through memory)
Here is a small example to show you what I mean : use the channel with buffer 1 to store the current number, select it from the channel when you need it, change it as you like, then return it for use by others.
cfstras
source share