struct Syn::Core::SpinLock

Overview

Tries to acquire an atomic lock by spining, trying to avoid slow thread context switches that involve the kernel scheduler but eventually fallback to yielding to another thread, to avoid spinning (and thus burning) a CPU that would be counter-productive (scheduler may believe the thread is doing some heavy computation and delay it's suspension).

This is a public alternative to the private Crystal::SpinLock in stdlib. That beind said, you're not supposed to ever need it.

The implementation is a NOOP until the program has enabling MT during compilation (i.e. -Dpreview_mt flag).

Defined in:

core/spin_lock.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def initialize #

[View source]
def lock : Nil #

[View source]
def synchronize(& : -> U) : U forall U #

Locks, yields and unlocks.

NOTE make sure that the block doesn't execute anything more than necessary, since concurrent threads will be blocked for its whole duration!


[View source]
def unlock : Nil #

[View source]