5 ms·
A spinlock is a kind of mutex (“mutual exclusion”) where in a condition is checked as fast as possible in a loop. This is useful when the expected wait times ar
by ptr 7y ago
A spinlock is a kind of mutex (“mutual exclusion”) where in a condition is checked as fast as possible in a loop. This is useful when the expected wait times are short as you don’t have to pay the context switch penalty. Typically done when you have real parallelism with multiple CPUs/cores.
A mutex can also refer to a particular group of “mutual exclusion” methods where in the current process/thread is suspended until the condition is true.
Hope this helps.
- jonny383 7y agoAdditional note that mutual exclusion is a concept that pops up a lot when dealing with concurrent processing (think threads, multi-process type architectures). Typically mutex / spinlock is implemented to reduce and prevent race conditions
- edoceo 7y agoOne can see this at a higher level in Golang, where a gofunc are reading/updating a map - and run with -race then it shows where you have to use a Mutex call to lock that Map for the access, R or RW. Higher level but pretty simple to see.
- zozbot234 7y agoOne can see this at an even higher level in Rust, where you don't even need a "race checker" to avoid data races in safe code - the compiler will force the use of patterns like Mutex<> or Rwlock<> (or others) for stuff that might be accessed concurrently in ways that would otherwise break mutual exclusion.
- jonny383 7y agoSo what would you call the application of the compiler enforcing non-race condition patterns of it's not a "race checker"? It's still there. It's just a compile-time "race checker", not runtime.
- tsimionescu 7y agoNot really, in Rust's case. It's the general borrow checker which handles this. Since in Rust it is illegal for a value to be referenced for writing in one place and referenced for anything else in any other place, regardless of concurrency/parallelism, Rust code is data-race free by design. I still think that GP's comment was a non-sequitur.
- zozbot234 7y agoRust has "interior" mutability, meaning that shared references are not true 'read only' references; they can be used for writing if the type supports such an operation. What makes Rust data-race free is the combination of 'interior' mutability and the Send and Sync traits, which control moving or referencing objects across threads.
- morelisp 7y agoRust's compiler checks a mathematical proof that, if your types are valid (e.g. you didn't do anything bad during `unsafe`) you do not have any data races. It is a guarantee about your program per se. Go's race detection says (with a high probability) you do or do not have any data races in the paths actually taken during some execution of your program. It is a (probabilistic) guarantee about how you ran your program.
- zozbot234 7y agoIt's not a mathematical proof in the literal sense (yet), because there are known soundness bugs in Rust that are yet to be addressed; moreover the interaction of borrow checking (including interior mutability) with the traits system is poorly understood and has not been proven to be correct even in a simplified model. It should yield a meaningful level of checking, and if nothing else, the ergonomics is way better than just running a static analysis on C/C++ code (setting aside the question of whether current static analysis tools can even check for possible data races); but you shouldn't rely on it to that extent.
- 7y ago
- edoceo 7y agoI was trying to show the problem-pattern space so others could learn. It's not about the wand, it's about showing the spells.