mutexes, etc

62 Aufrufe
Direkt zur ersten ungelesenen Nachricht

joe mcguckin

ungelesen,
17.07.2019, 20:52:3617.07.19
an golang-nuts
When do you need to use mutexes? Which begs the question: When can multiple coroutines trample on  memory or a variable?
Doesn't each instance of a go routine get it's own stack (so, it's own local copy of local vars). It would seem that the only
entities that it's possible to access from multiple goroutines are things allocated from the heap, as you mould have identical pointer 
values in multiple go threads - yes?  

Like they say, Inquiring minds want to know...

Thanks,

Joe

Michael Jones

ungelesen,
17.07.2019, 23:43:5717.07.19
an joe mcguckin, golang-nuts
Go has an unusual (clever!) property that anything whose address “escapes” from your routine will magically be allocated on the heap _no matter if it looks like heap or local allocation. Conversely, even explicit heap allocations that don’t escape will/may be local (will up to 10 MiB). 

Is data local or not. If yes, then it is safe and is as if a single thread program no matter how many CPUs or goroutines. This is a go glory. But if anything else can see it...then caution and precaution are necessary. 

--
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/a1fdcdb2-3276-4b67-88be-5414c12ccac5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael T. Jones
michae...@gmail.com
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten