0.2.0-dev Download ZIP
41 lines ·2.5 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="Mutex" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | A binary [Semaphore] for synchronization of multiple [Thread]s. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | A synchronization mutex (mutual exclusion). This is used to synchronize multiple [Thread]s, and is equivalent to a binary [Semaphore]. It guarantees that only one thread can access a critical section at a time. |
| 8 | This is a reentrant mutex, meaning that it can be locked multiple times by one thread, provided it also unlocks it as many times. |
| 9 | [b]Warning:[/b] To ensure proper cleanup without crashes or deadlocks, the following conditions must be met: |
| 10 | - When a [Mutex]'s reference count reaches zero and it is therefore destroyed, no threads (including the one on which the destruction will happen) must have it locked. |
| 11 | - When a [Thread]'s reference count reaches zero and it is therefore destroyed, it must not have any mutex locked. |
| 12 | </description> |
| 13 | <tutorials> |
| 14 | <link title="Using multiple threads">$DOCS_URL/tutorials/performance/using_multiple_threads.html</link> |
| 15 | <link title="Thread-safe APIs">$DOCS_URL/tutorials/performance/thread_safe_apis.html</link> |
| 16 | </tutorials> |
| 17 | <methods> |
| 18 | <method name="lock"> |
| 19 | <return type="void" /> |
| 20 | <description> |
| 21 | Locks this [Mutex], blocks until it is unlocked by the current owner. |
| 22 | [b]Note:[/b] This function returns without blocking if the thread already has ownership of the mutex. |
| 23 | </description> |
| 24 | </method> |
| 25 | <method name="try_lock"> |
| 26 | <return type="bool" /> |
| 27 | <description> |
| 28 | Tries locking this [Mutex], but does not block. Returns [code]true[/code] on success, [code]false[/code] otherwise. |
| 29 | [b]Note:[/b] This function returns [code]true[/code] if the thread already has ownership of the mutex. |
| 30 | </description> |
| 31 | </method> |
| 32 | <method name="unlock"> |
| 33 | <return type="void" /> |
| 34 | <description> |
| 35 | Unlocks this [Mutex], leaving it to other threads. |
| 36 | [b]Note:[/b] If a thread called [method lock] or [method try_lock] multiple times while already having ownership of the mutex, it must also call [method unlock] the same number of times in order to unlock it correctly. |
| 37 | [b]Warning:[/b] Calling [method unlock] more times than [method lock] on a given thread, thus ending up trying to unlock a non-locked mutex, is wrong and may causes crashes or deadlocks. |
| 38 | </description> |
| 39 | </method> |
| 40 | </methods> |
| 41 | </class> |