blob: 750d7215f6ce284881fb1bc9e9da6af734f44c91 (
plain)
1
2
3
4
5
6
7
8
9
|
<div class="section-level-extent" id="Synchronization"> <div class="nav-panel"> <p> Next: <a href="fast-enumeration" accesskey="n" rel="next">Fast Enumeration</a>, Previous: <a href="exceptions" accesskey="p" rel="prev">Exceptions</a>, Up: <a href="objective-c" accesskey="u" rel="up">GNU Objective-C Features</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Synchronization-1"><span>8.8 Synchronization<a class="copiable-link" href="#Synchronization-1"> ¶</a></span></h1> <p>GNU Objective-C provides support for synchronized blocks: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">@synchronized (ObjCClass *guard) {
…
}</pre>
</div> <p>Upon entering the <code class="code">@synchronized</code> block, a thread of execution shall first check whether a lock has been placed on the corresponding <code class="code">guard</code> object by another thread. If it has, the current thread shall wait until the other thread relinquishes its lock. Once <code class="code">guard</code> becomes available, the current thread will place its own lock on it, execute the code contained in the <code class="code">@synchronized</code> block, and finally relinquish the lock (thereby making <code class="code">guard</code> available to other threads). </p> <p>Unlike Java, Objective-C does not allow for entire methods to be marked <code class="code">@synchronized</code>. Note that throwing exceptions out of <code class="code">@synchronized</code> blocks is allowed, and will cause the guarding object to be unlocked properly. </p> <p>Because of the interactions between synchronization and exception handling, you can only use <code class="code">@synchronized</code> when compiling with exceptions enabled, that is with the command line option <samp class="option">-fobjc-exceptions</samp>. </p> </div><div class="_attribution">
<p class="_attribution-p">
© Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Synchronization.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Synchronization.html</a>
</p>
</div>
|