Paul,
thank you for the ideas. It helped.
I ended up with semi working version (http://play.golang.org/p/vlc4p9RB9W). "Semi" because it relies on the timer delay which is , well, just not good.
I'm not sure why this version (http://play.golang.org/p/Ey5eOubKpF) not working- I create a buffered channel, push values into it and then try to read them. Yet i'm missing something obvious and it i's throwing the "deadlock"
Thank you
AZ
Paul,
thank you for the ideas. It helped.
I ended up with semi working version (http://play.golang.org/p/vlc4p9RB9W). "Semi" because it relies on the timer delay which is , well, just not good.
I'm not sure why this version (http://play.golang.org/p/Ey5eOubKpF) not working- I create a buffered channel, push values into it and then try to read them. Yet i'm missing something obvious and it i's throwing the "deadlock
Thank you
AZ
On Wed, Jul 18, 2012 at 12:57 PM, Paul Borman wrote:
If it is an int type look at the sync/atomic package. If it is more complicated then you can use a sync.Mutex.
func (ms *MyStruct) IncCounter() int32 {return atomic.AddInt32(&ms.counter, 1)}
func (ms *MyStruct) IncCounter() int {ms.mu.Lock()defer ms.mu.Unlock()ms.counter++
return ms.counter}or, as in your direction, fire off a goroutine that reads a channel stored with ms. If you need the value it incremented to then you can make a channel that takes something like:type Request type {Delta MyTypeC chan MyType}and end up doing:r := Request{Delta: value; make(chan MyType)}ms.ch <- rnewValue := <- r.CHope this gives you some ideas-PaulPS: All the code above was typed into the mail not actually run.
i took an example from the "Effective go" and modified it by removed the explicit "close" http://play.golang.org/p/lAY7r4t_lr
now i enjoy seeing the deadlock on receiving channel.
So i _must explicitly_ close the channel for "Range" to stop reading on the channel? If that's true then how about the sutaiton when i have multiple routines running in parallel and having diffferent execution time... I would not know when the last one finished ...
what's the solution in this case? do i have to build a tracking mechanism just to identify that last routine has completed and i can close the channel?
please advise.
AZ
but why putting range into gor outine works ?
http://play.golang.org/p/4wZhG7y38r
but why putting range into gor outine works ?
http://play.golang.org/p/4wZhG7y38r
ohhh. what a shame... It was all in front of my eyes right from the beginning (Paul pointed me to this) and yet it took so many replies for me to finally "get it",
Thank you gentlemen for all your help!
That's what i ended up with (http://play.golang.org/p/WFt-xzcu-q)
But the rabbit's hole is deep :)
Now if i change the task slightly and will need to assign the initial value of the struct's counter to local counter, before increasing it:
[...]
andrey,
not sure - i want to pass same initial value to multiple goroutines. "do" seemed to serve for single call. like init .