20 lines
795 B
Markdown
20 lines
795 B
Markdown
# Problem 6.5: Implement a nonblocking spin lock
|
|
|
|
Spin locks are often used in code close to the hardware (e.g. in operating system kernels) to lock short
|
|
critical sections where the overhead of blocking and later unblocking the thread would cost more time
|
|
than the thread normally has to wait until entering the section. Instead of that, a spin lock contains a
|
|
busy waitig loop that repeats ("spins round and round") until it succeeds to acquire the lock.
|
|
|
|
Implement a spin lock as a Java class with the following structure and test it using the `SpinLockTest`
|
|
class in the source code folder on the server:
|
|
|
|
```java
|
|
public class SpinLock {
|
|
// ...
|
|
public void lock() {
|
|
// ...
|
|
}
|
|
public boolean unlock() { // returns whether the lock had indeed been acquired by current thread
|
|
// ...
|
|
}
|
|
} |