File: C:/Ruby27-x64/share/doc/ruby/html/Mutex.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class Mutex - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Object.html">Object</a>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-i-lock">#lock</a>
<li ><a href="#method-i-locked-3F">#locked?</a>
<li ><a href="#method-i-owned-3F">#owned?</a>
<li ><a href="#method-i-sleep">#sleep</a>
<li ><a href="#method-i-synchronize">#synchronize</a>
<li ><a href="#method-i-try_lock">#try_lock</a>
<li ><a href="#method-i-unlock">#unlock</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-Mutex">
<h1 id="class-Mutex" class="class">
class Mutex
</h1>
<section class="description">
<p><a href="Mutex.html"><code>Mutex</code></a> implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads.</p>
<p>Example:</p>
<pre class="ruby"><span class="ruby-identifier">semaphore</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">a</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> {
<span class="ruby-identifier">semaphore</span>.<span class="ruby-identifier">synchronize</span> {
<span class="ruby-comment"># access shared resource</span>
}
}
<span class="ruby-identifier">b</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> {
<span class="ruby-identifier">semaphore</span>.<span class="ruby-identifier">synchronize</span> {
<span class="ruby-comment"># access shared resource</span>
}
}
</pre>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new → mutex
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a new <a href="Mutex.html"><code>Mutex</code></a></p>
<div class="method-source-code" id="new-source">
<pre>static VALUE
mutex_initialize(VALUE self)
{
return self;
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-lock" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
lock → self
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Attempts to grab the lock and waits if it isn't available. Raises <code>ThreadError</code> if <code>mutex</code> was locked by the current thread.</p>
<div class="method-source-code" id="lock-source">
<pre>VALUE
rb_mutex_lock(VALUE self)
{
return do_mutex_lock(self, 1);
}</pre>
</div>
</div>
</div>
<div id="method-i-locked-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
locked? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if this lock is currently held by some thread.</p>
<div class="method-source-code" id="locked-3F-source">
<pre>VALUE
rb_mutex_locked_p(VALUE self)
{
rb_mutex_t *mutex = mutex_ptr(self);
return mutex->th ? Qtrue : Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-owned-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
owned? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if this lock is currently held by current thread.</p>
<div class="method-source-code" id="owned-3F-source">
<pre>VALUE
rb_mutex_owned_p(VALUE self)
{
rb_thread_t *th = GET_THREAD();
rb_mutex_t *mutex = mutex_ptr(self);
return mutex_owned_p(th, mutex);
}</pre>
</div>
</div>
</div>
<div id="method-i-sleep" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sleep(timeout = nil) → number
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Releases the lock and sleeps <code>timeout</code> seconds if it is given and non-nil or forever. Raises <code>ThreadError</code> if <code>mutex</code> wasn't locked by the current thread.</p>
<p>When the thread is next woken up, it will attempt to reacquire the lock.</p>
<p>Note that this method can wakeup without explicit <a href="Thread.html#method-i-wakeup"><code>Thread#wakeup</code></a> call. For example, receiving signal and so on.</p>
<div class="method-source-code" id="sleep-source">
<pre>static VALUE
mutex_sleep(int argc, VALUE *argv, VALUE self)
{
VALUE timeout;
timeout = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
return rb_mutex_sleep(self, timeout);
}</pre>
</div>
</div>
</div>
<div id="method-i-synchronize" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
synchronize { ... } → result of the block
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Obtains a lock, runs the block, and releases the lock when the block completes. See the example under <code>Mutex</code>.</p>
<div class="method-source-code" id="synchronize-source">
<pre>static VALUE
rb_mutex_synchronize_m(VALUE self)
{
if (!rb_block_given_p()) {
rb_raise(rb_eThreadError, "must be called with a block");
}
return rb_mutex_synchronize(self, rb_yield, Qundef);
}</pre>
</div>
</div>
</div>
<div id="method-i-try_lock" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
try_lock → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Attempts to obtain the lock and returns immediately. Returns <code>true</code> if the lock was granted.</p>
<div class="method-source-code" id="try_lock-source">
<pre>VALUE
rb_mutex_trylock(VALUE self)
{
rb_mutex_t *mutex = mutex_ptr(self);
VALUE locked = Qfalse;
if (mutex->th == 0) {
rb_thread_t *th = GET_THREAD();
mutex->th = th;
locked = Qtrue;
mutex_locked(th, self);
}
return locked;
}</pre>
</div>
</div>
</div>
<div id="method-i-unlock" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
unlock → self
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Releases the lock. Raises <code>ThreadError</code> if <code>mutex</code> wasn't locked by the current thread.</p>
<div class="method-source-code" id="unlock-source">
<pre>VALUE
rb_mutex_unlock(VALUE self)
{
const char *err;
rb_mutex_t *mutex = mutex_ptr(self);
err = rb_mutex_unlock_th(mutex, GET_THREAD());
if (err) rb_raise(rb_eThreadError, "%s", err);
return self;
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>