Scope/Implentation of Mutex

First @nathankerr: Sorry for not answering the last post (Template - Check if block is defined) I totally forgot about it and now I cannot answer anymore. I appretiate your answer - This looks helpful!

Back to topic:
Hi there,

I am working with mutexes for a while now but I do not really understand how they work. I checked the Code in the Go library but this did not help. Furthermore I just found articles about the usage and what it does on the surface (Locking - haha…).

Is there a resource which describes how it works under the hood? What is the scope, how is the lock protecting a variable etc.

1 Like

Udemy’s Todd Mcleod describes mutex and how it works.

Try here.

A mutex by itself does not protecting a variable. It just ensures that only a single goroutine can have the lock at the same time.

Nothing keeps you from accessing-the-variable-to-be-protected without getting the lock first.

In go (and most other languages I know) this is just convention and discipline. If we have an xmutex and an x, then we get the lock before accessing x.

1 Like

Thx for your responses! - I read this article and this is what I said: The article describes how to use locks and what they are doing but not how. How is the mutex influencing the access to state?

1 Like

sync package is part of Go’s core. Some of article I read that this is the new way of locking sensitive areas of code, and the old ways is using buffered channel. So I think if you need to know on how mutex lock works, try to implement using buffered channel.

The mutex itself does not influence access to state. Associating the mutex with the state to protect is accomplished entirely by the programmer by writing code that only accesses that state after acquiring a lock.


Ah… I get it, I guess. As long es I do not call Lock I could access value_to_protect without restrictions. It is like a breakpoint where the program continues only when no Lock has been acquired yet.

I thought all the time there is some runtime magic which determines the variables to protect :smiley:

1 Like

I didn’t like his tutorials at all :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.