132 lines
7.2 KiB
HTML
132 lines
7.2 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<!-- This manual documents Guile version 3.0.10.
|
|
|
|
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
|
|
Inc.
|
|
|
|
Copyright (C) 2021 Maxime Devos
|
|
|
|
Copyright (C) 2024 Tomas Volf
|
|
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
|
any later version published by the Free Software Foundation; with no
|
|
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
|
|
copy of the license is included in the section entitled "GNU Free
|
|
Documentation License." -->
|
|
<title>Thread Local Variables (Guile Reference Manual)</title>
|
|
|
|
<meta name="description" content="Thread Local Variables (Guile Reference Manual)">
|
|
<meta name="keywords" content="Thread Local Variables (Guile Reference Manual)">
|
|
<meta name="resource-type" content="document">
|
|
<meta name="distribution" content="global">
|
|
<meta name="Generator" content=".texi2any-real">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
|
|
<link href="index.html" rel="start" title="Top">
|
|
<link href="Concept-Index.html" rel="index" title="Concept Index">
|
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
|
<link href="Scheduling.html" rel="up" title="Scheduling">
|
|
<link href="Asyncs.html" rel="next" title="Asyncs">
|
|
<link href="Threads.html" rel="prev" title="Threads">
|
|
<style type="text/css">
|
|
<!--
|
|
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
|
|
div.example {margin-left: 3.2em}
|
|
span:hover a.copiable-link {visibility: visible}
|
|
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
|
|
-->
|
|
</style>
|
|
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
|
|
|
|
|
|
</head>
|
|
|
|
<body lang="en">
|
|
<div class="subsection-level-extent" id="Thread-Local-Variables">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="Asyncs.html" accesskey="n" rel="next">Asynchronous Interrupts</a>, Previous: <a href="Threads.html" accesskey="p" rel="prev">Threads</a>, Up: <a href="Scheduling.html" accesskey="u" rel="up">Threads, Mutexes, Asyncs and Dynamic Roots</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<hr>
|
|
<h4 class="subsection" id="Thread_002dLocal-Variables"><span>6.22.2 Thread-Local Variables<a class="copiable-link" href="#Thread_002dLocal-Variables"> ¶</a></span></h4>
|
|
|
|
<p>Sometimes you want to establish a variable binding that is only valid
|
|
for a given thread: a “thread-local variable”.
|
|
</p>
|
|
<p>You would think that fluids or parameters would be Guile’s answer for
|
|
thread-local variables, since establishing a new fluid binding doesn’t
|
|
affect bindings in other threads. See <a class="xref" href="Fluids-and-Dynamic-States.html">Fluids and Dynamic States</a>, or
|
|
See <a class="xref" href="Parameters.html">Parameters</a>. However, new threads inherit the fluid bindings that
|
|
were in place in their creator threads. In this way, a binding
|
|
established using a fluid (or a parameter) in a thread can escape to
|
|
other threads, which might not be what you want. Or, it might escape
|
|
via explicit reification via <code class="code">current-dynamic-state</code>.
|
|
</p>
|
|
<p>Of course, this dynamic scoping might be exactly what you want; that’s
|
|
why fluids and parameters work this way, and is what you want for
|
|
many common parameters such as the current input and output ports, the
|
|
current locale conversion parameters, and the like. Perhaps this is the
|
|
case for most parameters, even. If your use case for thread-local
|
|
bindings comes from a desire to isolate a binding from its setting in
|
|
unrelated threads, then fluids and parameters apply nicely.
|
|
</p>
|
|
<p>On the other hand, if your use case is to prevent concurrent access to a
|
|
value from multiple threads, then using vanilla fluids or parameters is
|
|
not appropriate. For this purpose, Guile has <em class="dfn">thread-local fluids</em>.
|
|
A fluid created with <code class="code">make-thread-local-fluid</code> won’t be captured by
|
|
<code class="code">current-dynamic-state</code> and won’t be propagated to new threads.
|
|
</p>
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-make_002dthread_002dlocal_002dfluid"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">make-thread-local-fluid</strong> <var class="def-var-arguments">[dflt]</var><a class="copiable-link" href="#index-make_002dthread_002dlocal_002dfluid"> ¶</a></span></dt>
|
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fmake_005fthread_005flocal_005ffluid"><span class="category-def">C Function: </span><span><strong class="def-name">scm_make_thread_local_fluid</strong> <var class="def-var-arguments">(dflt)</var><a class="copiable-link" href="#index-scm_005fmake_005fthread_005flocal_005ffluid"> ¶</a></span></dt>
|
|
<dd><p>Return a newly created fluid, whose initial value is <var class="var">dflt</var>, or
|
|
<code class="code">#f</code> if <var class="var">dflt</var> is not given. Unlike fluids made with
|
|
<code class="code">make-fluid</code>, thread local fluids are not captured by
|
|
<code class="code">make-dynamic-state</code>. Similarly, a newly spawned child thread does
|
|
not inherit thread-local fluid values from the parent thread.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-fluid_002dthread_002dlocal_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">fluid-thread-local?</strong> <var class="def-var-arguments">fluid</var><a class="copiable-link" href="#index-fluid_002dthread_002dlocal_003f"> ¶</a></span></dt>
|
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005ffluid_005fthread_005flocal_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_fluid_thread_local_p</strong> <var class="def-var-arguments">(fluid)</var><a class="copiable-link" href="#index-scm_005ffluid_005fthread_005flocal_005fp"> ¶</a></span></dt>
|
|
<dd><p>Return <code class="code">#t</code> if the fluid <var class="var">fluid</var> is thread-local, or
|
|
<code class="code">#f</code> otherwise.
|
|
</p></dd></dl>
|
|
|
|
<p>For example:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">(define %thread-local (make-thread-local-fluid))
|
|
|
|
(with-fluids ((%thread-local (compute-data)))
|
|
... (fluid-ref %thread-local) ...)
|
|
</pre></div>
|
|
|
|
<p>You can also make a thread-local parameter out of a thread-local fluid
|
|
using the normal <code class="code">fluid->parameter</code>:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">(define param (fluid->parameter (make-thread-local-fluid)))
|
|
|
|
(parameterize ((param (compute-data)))
|
|
... (param) ...)
|
|
</pre></div>
|
|
|
|
|
|
</div>
|
|
<hr>
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="Asyncs.html">Asynchronous Interrupts</a>, Previous: <a href="Threads.html">Threads</a>, Up: <a href="Scheduling.html">Threads, Mutexes, Asyncs and Dynamic Roots</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|