mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-16 15:56:21 -05:00
7 lines
No EOL
15 KiB
HTML
7 lines
No EOL
15 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Thread (ocaml.Thread)</title><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../index.html">ocaml</a> » Thread</nav><header class="odoc-preamble"><h1>Module <code><span>Thread</span></code></h1><p>Lightweight threads for Posix <code>1003.1c</code> and Win32.</p></header><nav class="odoc-toc"><ul><li><a href="#thread-creation-and-termination">Thread creation and termination</a></li><li><a href="#suspending-threads">Suspending threads</a></li><li><a href="#waiting-for-file-descriptors-or-processes">Waiting for file descriptors or processes</a></li><li><a href="#management-of-signals">Management of signals</a></li><li><a href="#uncaught-exceptions">Uncaught exceptions</a></li></ul></nav><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span></code></div><div class="spec-doc"><p>The type of thread handles.</p></div></div><h2 id="thread-creation-and-termination"><a href="#thread-creation-and-termination" class="anchor"></a>Thread creation and termination</h2><div class="odoc-spec"><div class="spec value anchored" id="val-create"><a href="#val-create" class="anchor"></a><code><span><span class="keyword">val</span> create : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span class="type-var">'b</span>)</span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>Thread.create funct arg</code> creates a new thread of control, in which the function application <code>funct arg</code> is executed concurrently with the other threads of the domain. The application of <code>Thread.create</code> returns the handle of the newly created thread. The new thread terminates when the application <code>funct arg</code> returns, either normally or by raising the <a href="#exception-Exit"><code>Thread.Exit</code></a> exception or by raising any other uncaught exception. In the last case, the uncaught exception is printed on standard error, but not propagated back to the parent thread. Similarly, the result of the application <code>funct arg</code> is discarded and not directly accessible to the parent thread.</p><p>See also <code>Domain</code>.spawn if you want parallel execution instead.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-self"><a href="#val-self" class="anchor"></a><code><span><span class="keyword">val</span> self : <span>unit <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Return the handle for the thread currently executing.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-id"><a href="#val-id" class="anchor"></a><code><span><span class="keyword">val</span> id : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> int</span></code></div><div class="spec-doc"><p>Return the identifier of the given thread. A thread identifier is an integer that identifies uniquely the thread. It can be used to build data structures indexed by threads.</p></div></div><div class="odoc-spec"><div class="spec exception anchored" id="exception-Exit"><a href="#exception-Exit" class="anchor"></a><code><span><span class="keyword">exception</span> </span><span><span class="exception">Exit</span></span></code></div><div class="spec-doc"><p>Exception raised by user code to initiate termination of the current thread. In a thread created by <a href="#val-create"><code>Thread.create</code></a> <code>funct</code> <code>arg</code>, if the <a href="#exception-Exit"><code>Thread.Exit</code></a> exception reaches the top of the application <code>funct arg</code>, it has the effect of terminating the current thread silently. In other contexts, there is no implicit handling of the <a href="#exception-Exit"><code>Thread.Exit</code></a> exception.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-exit"><a href="#val-exit" class="anchor"></a><code><span><span class="keyword">val</span> exit : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Raise the <a href="#exception-Exit"><code>Thread.Exit</code></a> exception. In a thread created by <a href="#val-create"><code>Thread.create</code></a>, this will cause the thread to terminate prematurely, unless the thread function handles the exception itself. <code>Fun</code>.protect finalizers and catch-all exception handlers will be executed.</p><p>To make it clear that an exception is raised and will trigger finalizers and catch-all exception handlers, it is recommended to write <code>raise Thread.Exit</code> instead of <code>Thread.exit ()</code>.</p><ul class="at-tags"><li class="before"><span class="at-tag">before</span> <span class="value">5.0</span> <p>A different implementation was used, not based on raising an exception, and not running finalizers and catch-all handlers. The previous implementation had a different behavior when called outside of a thread created by <a href="#val-create"><code>Thread.create</code></a>.</p></li></ul><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> Use 'raise Thread.Exit' instead.</li></ul></div></div><h2 id="suspending-threads"><a href="#suspending-threads" class="anchor"></a>Suspending threads</h2><div class="odoc-spec"><div class="spec value anchored" id="val-delay"><a href="#val-delay" class="anchor"></a><code><span><span class="keyword">val</span> delay : <span>float <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>delay d</code> suspends the execution of the calling thread for <code>d</code> seconds. The other program threads continue to run during this time.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-join"><a href="#val-join" class="anchor"></a><code><span><span class="keyword">val</span> join : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>join th</code> suspends the execution of the calling thread until the thread <code>th</code> has terminated.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-yield"><a href="#val-yield" class="anchor"></a><code><span><span class="keyword">val</span> yield : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Re-schedule the calling thread without suspending it. This function can be used to give scheduling hints, telling the scheduler that now is a good time to switch to other threads.</p></div></div><h2 id="waiting-for-file-descriptors-or-processes"><a href="#waiting-for-file-descriptors-or-processes" class="anchor"></a>Waiting for file descriptors or processes</h2><p>The functions below are leftovers from an earlier, VM-based threading system. The <a href="../Unix/index.html"><code>Unix</code></a> module provides equivalent functionality, in a more general and more standard-conformant manner. It is recommended to use <a href="../Unix/index.html"><code>Unix</code></a> functions directly.</p><div class="odoc-spec"><div class="spec value anchored" id="val-wait_timed_read"><a href="#val-wait_timed_read" class="anchor"></a><code><span><span class="keyword">val</span> wait_timed_read : <span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>See <a href="#val-wait_timed_write"><code>Thread.wait_timed_write</code></a>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> Use Unix.select instead.</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-wait_timed_write"><a href="#val-wait_timed_write" class="anchor"></a><code><span><span class="keyword">val</span> wait_timed_write : <span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>Suspend the execution of the calling thread until at least one character or EOF is available for reading (<code>wait_timed_read</code>) or one character can be written without blocking (<code>wait_timed_write</code>) on the given Unix file descriptor. Wait for at most the amount of time given as second argument (in seconds). Return <code>true</code> if the file descriptor is ready for input/output and <code>false</code> if the timeout expired. The same functionality can be achieved with <a href="../Unix/index.html#val-select"><code>Unix.select</code></a>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> Use Unix.select instead.</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-select"><a href="#val-select" class="anchor"></a><code><span><span class="keyword">val</span> select :
|
||
<span><span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> list</span> <span class="arrow">-></span></span>
|
||
<span><span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> list</span> <span class="arrow">-></span></span>
|
||
<span><span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> list</span> <span class="arrow">-></span></span>
|
||
<span>float <span class="arrow">-></span></span>
|
||
<span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> list</span> * <span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> list</span> * <span><a href="../Unix/index.html#type-file_descr">Unix.file_descr</a> list</span></span></code></div><div class="spec-doc"><p>Same function as <a href="../Unix/index.html#val-select"><code>Unix.select</code></a>. Suspend the execution of the calling thread until input/output becomes possible on the given Unix file descriptors. The arguments and results have the same meaning as for <a href="../Unix/index.html#val-select"><code>Unix.select</code></a>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> Use Unix.select instead.</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-wait_pid"><a href="#val-wait_pid" class="anchor"></a><code><span><span class="keyword">val</span> wait_pid : <span>int <span class="arrow">-></span></span> int * <a href="../Unix/index.html#type-process_status">Unix.process_status</a></span></code></div><div class="spec-doc"><p>Same function as <a href="../Unix/index.html#val-waitpid"><code>Unix.waitpid</code></a>. <code>wait_pid p</code> suspends the execution of the calling thread until the process specified by the process identifier <code>p</code> terminates. Returns the pid of the child caught and its termination status, as per <a href="../Unix/index.html#val-wait"><code>Unix.wait</code></a>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> Use Unix.waitpid instead.</li></ul></div></div><h2 id="management-of-signals"><a href="#management-of-signals" class="anchor"></a>Management of signals</h2><p>Signal handling follows the POSIX thread model: signals generated by a thread are delivered to that thread; signals generated externally are delivered to one of the threads that does not block it. Each thread possesses a set of blocked signals, which can be modified using <a href="#val-sigmask"><code>Thread.sigmask</code></a>. This set is inherited at thread creation time. Per-thread signal masks are supported only by the system thread library under Unix, but not under Win32, nor by the VM thread library.</p><div class="odoc-spec"><div class="spec value anchored" id="val-sigmask"><a href="#val-sigmask" class="anchor"></a><code><span><span class="keyword">val</span> sigmask : <span><a href="../Unix/index.html#type-sigprocmask_command">Unix.sigprocmask_command</a> <span class="arrow">-></span></span> <span><span>int list</span> <span class="arrow">-></span></span> <span>int list</span></span></code></div><div class="spec-doc"><p><code>sigmask cmd sigs</code> changes the set of blocked signals for the calling thread. If <code>cmd</code> is <code>SIG_SETMASK</code>, blocked signals are set to those in the list <code>sigs</code>. If <code>cmd</code> is <code>SIG_BLOCK</code>, the signals in <code>sigs</code> are added to the set of blocked signals. If <code>cmd</code> is <code>SIG_UNBLOCK</code>, the signals in <code>sigs</code> are removed from the set of blocked signals. <code>sigmask</code> returns the set of previously blocked signals for the thread.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-wait_signal"><a href="#val-wait_signal" class="anchor"></a><code><span><span class="keyword">val</span> wait_signal : <span><span>int list</span> <span class="arrow">-></span></span> int</span></code></div><div class="spec-doc"><p><code>wait_signal sigs</code> suspends the execution of the calling thread until the process receives one of the signals specified in the list <code>sigs</code>. It then returns the number of the signal received. Signal handlers attached to the signals in <code>sigs</code> will not be invoked. The signals <code>sigs</code> are expected to be blocked before calling <code>wait_signal</code>.</p></div></div><h2 id="uncaught-exceptions"><a href="#uncaught-exceptions" class="anchor"></a>Uncaught exceptions</h2><div class="odoc-spec"><div class="spec value anchored" id="val-default_uncaught_exception_handler"><a href="#val-default_uncaught_exception_handler" class="anchor"></a><code><span><span class="keyword">val</span> default_uncaught_exception_handler : <span>exn <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>Thread.default_uncaught_exception_handler</code> will print the thread's id, exception and backtrace (if available).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_uncaught_exception_handler"><a href="#val-set_uncaught_exception_handler" class="anchor"></a><code><span><span class="keyword">val</span> set_uncaught_exception_handler : <span><span>(<span>exn <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>Thread.set_uncaught_exception_handler fn</code> registers <code>fn</code> as the handler for uncaught exceptions.</p><p>If the newly set uncaught exception handler raise an exception, <a href="#val-default_uncaught_exception_handler"><code>default_uncaught_exception_handler</code></a> will be called.</p></div></div></div></body></html> |