4 ms·
N00b question here but when people refer to mutex's, they are usually talking about multiple cores on a single machine right? Isn't there a different terminolo
by epberry 11y ago
N00b question here but when people refer to mutex's, they are usually talking about multiple cores on a single machine right? Isn't there a different terminology for locks taken across the network in distributed systems?
- Matt3o12_ 11y agoNo. There are usually talking about 2 threads accessing the same code. Let's suppose we have an int, and want to add 2, we would do: 1. int i = sharedInt 2. i = i + 2 3. sharedInt = i (that's what the compile actually does when you write sharedInt += 2). If the runtime decides two stop the current thread on line 2 and lets the second one run all three lines, we would have a problem if I didn't use a mutex.
- hurin 11y agoYou can compile on x86 for atomic increments? (Go seems to support it https://golang.org/pkg/sync/atomic/ https://golang.org/pkg/sync/atomic/)
- Matt3o12_ 11y agoI don't know but I think so. I just wanted to give him an oversimplified example (you could also use any complicated computation). I'm not sure, though, if every compiler/interpreter makes use of them (what about VMs)?
- jzelinskie 11y agoThis is available only under certain architectures (x86 is one of them as you've noted), but I've also seen scenarios where the atomic increment instruction is actually slower than using a mutex. Don't consider this feature to be a magic bullet and always test your use cases! As for the parent discussion, usually a mutex is talking about the threading construct while a "lock service" is how you'd refer to something like etcd or zookeeper.
- hurin 11y agoHow could it be slower than a mutex? Is it from compiling all your increments to use LOCK? What exactly were you timing? That sounds really strange..
- YZF 11y agoMutexes are typically implemented over atomic instructions. So you'd do something like atomic compare/exchange to acquire the mutex and if there's no contention you got it. If there is contention you go to the OSes synchronization constructs which are typically much slower... An atomic increment should always be faster than acquiring a mutex and incrementing...
- bottled_poe 11y ago> No. There are usually talking about 2 threads accessing the same code. Don't you mean two threads accessing the same data? Mutexes are about data protection. This usually manifests as exclusive exectution of code, but it is not the purpose.
- yason 11y agoYes¹. I don't think I've ever heard of using the word mutex, namely, for synchronizing access to data over a network. Mutex kind of implies that there's the synchronization primitive and there's the data, and both can be touched separately but it's just an agreement that we touch the mutex first. However, network protocols usually just transfer updates to/from the data and then let the server do the synchronization and access pairing for each connection. I don't think it would make sense at all to run a "mutex service" on one server and asking clients to grab that mutex before accessing another "data service" which would obviously allow any data updates by anyone without any synchronization at all. ¹ or just multiple tasks, in general. Even a single-core machine needs mutexed access to shared data because several tasks could try to concurrently use that memory location and the scheduler could switch tasks at a critical point. However, if strictly limited to single-core, and the system allows to, you can just disable interrupts while you're manipulating the data.
- twic 11y ago> I don't think it would make sense at all to run a "mutex service" on one server and asking clients to grab that mutex before accessing another "data service" And yet such things exist: http://en.m.wikipedia.org/wiki/Distributed_lock_manager http://en.m.wikipedia.org/wiki/Distributed_lock_manager I first came across one in the context of a distributed cache which sat on top of a database. I'm not at all convinced it was a good design, but it was there!