V
V
Vladimir Grabko2016-06-03 23:59:39
go
Vladimir Grabko, 2016-06-03 23:59:39

Is it possible to put a sync lock on a struct?

I once saw that you can make a read / write lock directly in the structure and then the variable that will have the structure type will always be thread-safe. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vaavaan, 2016-06-04
@VGrabko

Not in Go.
In Go, you must explicitly check for this.
You can hang Mutex inside, but you still need to check it. Inside, next to the data, it may be conveniently stored. Only and everything. It will not work automatically.
You can use the lock-free ideology. Then you don't need to save anything. And the data itself will be blocking elements. But even in this case, you need to explicitly check them. This is the atomic package in Go.
But if you put the structure in the channel, then yes, here you will get thread safety. But it's not exactly what you described.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question