Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Thread::Mutex#try_lock
mutex.try_lock → true or false
thread_sync.c
View on GitHub ↗
VALUE
rb_mutex_trylock(VALUE self)
{
rb_mutex_t *mutex = mutex_ptr(self);
if (mutex->fiber == 0) {
RUBY_DEBUG_LOG("%p ok", mutex);
rb_fiber_t *fiber = GET_EC()->fiber_ptr;
rb_thread_t *th = GET_THREAD();
mutex->fiber = fiber;
mutex_locked(th, self);
return Qtrue;
}
else {
RUBY_DEBUG_LOG("%p ng", mutex);
return Qfalse;
}
}
Attempts to obtain the lock and returns immediately. Returns true if the lock was granted.