Thread safety. You can lock an object to make global?

Let's say I create a global variable
var maxDataLock sync.Mutex
now I can use it
maxDataLock.Lock()
Event[name](ps)
maxDataLock.Unlock()

So the correct block or for each function you need your variable to do?
July 9th 19 at 13:49
2 answers
July 9th 19 at 13:51
Solution
Lock have to compete with the race appeals to the same data. Not to be used for independent operations on the same lock, it may cause performance degradation. There are other nuances, but this is important. Don't forget to read about deathlok and atomic operations, there are still freelock. I hope have correctly understood the question.:-)
Unlock should always use a try finally or destroctor - mercedes commented on July 9th 19 at 13:54
:
package events

import (
"sync"
)

var Event map[string]func(ps []string)
var maxDataLock sync.Mutex

func init() {
 Event = make(map[string]func(ps []string))
 maxDataLock = &sync.Mutex{}
}

// Subscribe to event
func Add(name string, callback func(ps []string)) {
maxDataLock.Lock()
 Event[name] = callback
maxDataLock.Unlock()
}

// Call event
func Call(name string ps []string) {
maxDataLock.Lock()
Event[name](ps)
maxDataLock.Unlock()
}


So the correct lock? - Reta76 commented on July 9th 19 at 13:57
Event[name](ps) if this call will throw an exception,then the unlock will never be invoked, on the GO did not write, will not prompt for the code, need design type of the try finally block in C++/c#/java etc. I Read on Google, like here, this is not in the same form. Here is the craftsman tried https://github.com/manucorporat/try - mercedes commented on July 9th 19 at 14:00
2. Event[name](ps) What will happen if it's not listed in the dictionary of callback? The exception? Need check. - mercedes commented on July 9th 19 at 14:03
type MyType struct {
sync.RWMutex
 d map[string]string
}

func (m *MyType) Add(key, value string) {
m.Lock()
 defer m.Unlock()
 d[key] = value
 // ... any code that can drop, defer m.Unlock() will correct the situation and unlocks, but, of course, if you do not handle the defer/recover the fall in panic will still carry out the program:)
}
- Alyson.McClure commented on July 9th 19 at 14:09
Something like this (in rŠµcover\panic did not penetrate), why do You GO, if not a secret? - mercedes commented on July 9th 19 at 14:12
: after PHP is like a breath of fresh air, and in comparison with the advantages it is much simpler and there are the ports of C lib - Reta76 commented on July 9th 19 at 14:15
: thanks! - Reta76 commented on July 9th 19 at 14:18
Please. Suggest to look at alternatives GO, based on Your objectives, and read about synchronization in the future. Good luck. - mercedes commented on July 9th 19 at 14:21
: what alternatives would you recommend? - Reta76 commented on July 9th 19 at 14:24
On mail see "why the ugly try catch if from the box it's fine," Then why not. Not familiar with go, he came to a defer, while looking for an alternative to try/finally in Google. About alternatives to language. What tasks and under what OS ? - mercedes commented on July 9th 19 at 14:27
: Yes, anything. From system programming to web - Reta76 commented on July 9th 19 at 14:30
: C# where possible and convenient. Plus it tends to be cross-platform. I love c# because he typed because he's a meta,because I can compile on the fly c#because I started with C/C++. For scripting and functional languages do not tell, do not have such long tasks to do. In some problems the last very comfortable.
The main thing to understand what you are doing, if you have time, you can learn alternate languages at different levels. All languages and frameworks have limitations in principle and in specific tasks. - mercedes commented on July 9th 19 at 14:33
: Golang very Mature programming language, as the opportunities and infrastructure and quality of already written code.

Cons not so much:
transitioning to compile Golang himself with si - slightly increased the binaries and decreased execution speed, but with each version already is approaching that of version 1.4, and in some tests is superior.
- GC (garbage collector, the garbage collector) is not suitable for all, there is no way to abandon it entirely, and to use the memory using unsafe is not so convenient and safe.
- previously, large GC pause with a large number of pointers, for now (with 1.6), the scheduler garbage collector runs in the background and pause does not exceed 10ms
- relatively large binaries
- there is no possibility of dynamic extension libraries (in other words in the running application you can't do Eval(code) without writing a heap of code, and not canonical it is) - Alyson.McClure commented on July 9th 19 at 14:36
: I read, thank you. - mercedes commented on July 9th 19 at 14:39
another minus I remember - not very good support for iOS and Android and is really not a normal library for cross-platform GUI, under the periphery of the articles is also little or no (e.g. found one library which should be able to work with audio microphone in MacOS, but have not seen any more articles on this topic and while the library itself did not check)

Poreklamirovat us C#. Just this morning I watched the report of the Evangelist from Microsoft about machine learning in F#, it was interesting, in appearance very "scripting" language and perfectly placed on something that would "calculate something", especially if there is klasster. But these footcloths test - is not entirely clear. In contrast, in Golang tests are written just elementary and not write them no reason. - Alyson.McClure commented on July 9th 19 at 14:42
: Cross-platform, minus C#? Yes, already wrote above. About the tests do not quite understand. Problems in tests or in tests of the implementation of the report? - mercedes commented on July 9th 19 at 14:45
: >>Event[name](ps) What will happen if it's not listed in the dictionary of callback? The exception? Need check.
In Go there are no exceptions, you need something like:
f, err := Event[name]
And process err.
The language of the other, really. Sharp is very good, but away from Windows do not leave, no matter how tried all these ports to other platforms is a serious, but toys. - ephraim_Schumm commented on July 9th 19 at 14:48
: thank you for the clarification. About the porting - time will tell, I will not argue. - mercedes commented on July 9th 19 at 14:51
What prevents to move away from Windows? - mercedes commented on July 9th 19 at 14:54
:
another minus I remember - not very good support for iOS and Android and is really not a normal library for cross-platform GUI, under the periphery of the articles is also little or no (e.g. found one library which should be able to work with audio microphone in MacOS, but have not seen any more articles on this topic and while the library itself did not check)
I mean Golang said. - Alyson.McClure commented on July 9th 19 at 14:57
: Android very well - Reta76 commented on July 9th 19 at 15:00
: Give links to proof which describes from a practical point of view:) Especially interesting: GUI, hardware features (accelerometer, audio, video and stuff like that) and preferably without Java:) - Alyson.McClure commented on July 9th 19 at 15:03
: https://google.gik-team.com/?q=Golang+%D0%B0%D0%BD... - Reta76 commented on July 9th 19 at 15:06
: the fact that you showed I read:) And I see there is a decent amount of problems associated with the inconvenience and, often, the presence of the Java runtime and our two and all that. You mean a native version, tell us:) - Alyson.McClure commented on July 9th 19 at 15:09
: you want a language which is made for network utilities to write GUI? clever. What can I say)

But seriously it say that in the future, Golang 5 replace Java in vedriano - Reta76 commented on July 9th 19 at 15:12
: What makes you think that Golang was developed, quote, "network utilities". And Yes, I wish that it were possible to do a native app for all platforms what is interesting to me, without crutches and ligaments with other languages. So cut the arrogance. - Alyson.McClure commented on July 9th 19 at 15:15
it's his niche so to speak. I also want this. I apologize for the arrogance - Reta76 commented on July 9th 19 at 15:18
July 9th 19 at 13:53
If they are different mutexes, they are different lock give.
Or to bring it all to call one common function (in which the block occurs).
Or if the code handles locked objects is difficult - use channels.
Or, use lock-free algorithms using atomic package. Read about it in a series of articles https://habrahabr.ru/post/196834

Find more questions by tags Go