2017-09-13 04:42:36 +08:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 16:08:05 +08:00
<class name= "Semaphore" inherits= "RefCounted" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2017-09-13 04:42:36 +08:00
<brief_description >
2023-04-28 07:35:33 +08:00
A synchronization mechanism used to control access to a shared resource by [Thread]s.
2017-09-13 04:42:36 +08:00
</brief_description>
<description >
2023-04-28 07:35:33 +08:00
A synchronization semaphore that can be used to synchronize multiple [Thread]s. Initialized to zero on creation. For a binary version, see [Mutex].
[b]Warning:[/b] Semaphores must be used carefully to avoid deadlocks.
[b]Warning:[/b] To guarantee that the operating system is able to perform proper cleanup (no crashes, no deadlocks), these conditions must be met:
- When a [Semaphore]'s reference count reaches zero and it is therefore destroyed, no threads must be waiting on it.
- When a [Thread]'s reference count reaches zero and it is therefore destroyed, it must not be waiting on any semaphore.
2017-09-13 04:42:36 +08:00
</description>
<tutorials >
2021-11-15 17:43:07 +08:00
<link title= "Using multiple threads" > $DOCS_URL/tutorials/performance/using_multiple_threads.html</link>
2023-04-28 07:35:33 +08:00
<link title= "Thread-safe APIs" > $DOCS_URL/tutorials/performance/thread_safe_apis.html</link>
2017-09-13 04:42:36 +08:00
</tutorials>
<methods >
<method name= "post" >
2021-07-30 21:28:05 +08:00
<return type= "void" />
2017-09-13 04:42:36 +08:00
<description >
2020-03-03 16:26:42 +08:00
Lowers the [Semaphore], allowing one more thread in.
2017-09-13 04:42:36 +08:00
</description>
</method>
2020-03-09 17:12:53 +08:00
<method name= "try_wait" >
2023-01-27 18:04:41 +08:00
<return type= "bool" />
2020-03-03 16:26:42 +08:00
<description >
2023-01-27 18:04:41 +08:00
Like [method wait], but won't block, so if the value is zero, fails immediately and returns [code]false[/code]. If non-zero, it returns [code]true[/code] to report success.
2020-03-03 16:26:42 +08:00
</description>
</method>
2020-03-09 17:12:53 +08:00
<method name= "wait" >
2021-07-30 21:28:05 +08:00
<return type= "void" />
2017-09-13 04:42:36 +08:00
<description >
2020-03-09 17:12:53 +08:00
Waits for the [Semaphore], if its value is zero, blocks until non-zero.
2017-09-13 04:42:36 +08:00
</description>
</method>
</methods>
</class>