2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
11 GNU Objective-C provides support for synchronized blocks:
13 .. code-block:: objective-c
15 @synchronized (ObjCClass *guard) {
19 Upon entering the ``@synchronized`` block, a thread of execution
20 shall first check whether a lock has been placed on the corresponding
21 ``guard`` object by another thread. If it has, the current thread
22 shall wait until the other thread relinquishes its lock. Once
23 ``guard`` becomes available, the current thread will place its own
24 lock on it, execute the code contained in the ``@synchronized``
25 block, and finally relinquish the lock (thereby making ``guard``
26 available to other threads).
28 Unlike Java, Objective-C does not allow for entire methods to be
29 marked ``@synchronized``. Note that throwing exceptions out of
30 ``@synchronized`` blocks is allowed, and will cause the guarding
31 object to be unlocked properly.
33 Because of the interactions between synchronization and exception
34 handling, you can only use ``@synchronized`` when compiling with
35 exceptions enabled, that is with the command line option
36 :option:`-fobjc-exceptions`.