CINXE.COM

threads::shared - Perl extension for sharing data structures between threads - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>threads::shared - Perl extension for sharing data structures between threads - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/threads::shared"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.28.3</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/threads::shared">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/threads::shared">5.40.1</a> <a class="dropdown-item" href="/5.40.0/threads::shared">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/threads::shared">5.38.3</a> <a class="dropdown-item" href="/5.38.2/threads::shared">5.38.2</a> <a class="dropdown-item" href="/5.38.1/threads::shared">5.38.1</a> <a class="dropdown-item" href="/5.38.0/threads::shared">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/threads::shared">5.36.3</a> <a class="dropdown-item" href="/5.36.2/threads::shared">5.36.2</a> <a class="dropdown-item" href="/5.36.1/threads::shared">5.36.1</a> <a class="dropdown-item" href="/5.36.0/threads::shared">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/threads::shared">5.34.3</a> <a class="dropdown-item" href="/5.34.2/threads::shared">5.34.2</a> <a class="dropdown-item" href="/5.34.1/threads::shared">5.34.1</a> <a class="dropdown-item" href="/5.34.0/threads::shared">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/threads::shared">5.32.1</a> <a class="dropdown-item" href="/5.32.0/threads::shared">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/threads::shared">5.30.3</a> <a class="dropdown-item" href="/5.30.2/threads::shared">5.30.2</a> <a class="dropdown-item" href="/5.30.1/threads::shared">5.30.1</a> <a class="dropdown-item" href="/5.30.0/threads::shared">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.28.3/threads::shared">5.28.3</a> <a class="dropdown-item" href="/5.28.2/threads::shared">5.28.2</a> <a class="dropdown-item" href="/5.28.1/threads::shared">5.28.1</a> <a class="dropdown-item" href="/5.28.0/threads::shared">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/threads::shared">5.26.3</a> <a class="dropdown-item" href="/5.26.2/threads::shared">5.26.2</a> <a class="dropdown-item" href="/5.26.1/threads::shared">5.26.1</a> <a class="dropdown-item" href="/5.26.0/threads::shared">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/threads::shared">5.24.4</a> <a class="dropdown-item" href="/5.24.3/threads::shared">5.24.3</a> <a class="dropdown-item" href="/5.24.2/threads::shared">5.24.2</a> <a class="dropdown-item" href="/5.24.1/threads::shared">5.24.1</a> <a class="dropdown-item" href="/5.24.0/threads::shared">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/threads::shared">5.22.4</a> <a class="dropdown-item" href="/5.22.3/threads::shared">5.22.3</a> <a class="dropdown-item" href="/5.22.2/threads::shared">5.22.2</a> <a class="dropdown-item" href="/5.22.1/threads::shared">5.22.1</a> <a class="dropdown-item" href="/5.22.0/threads::shared">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/threads::shared">5.20.3</a> <a class="dropdown-item" href="/5.20.2/threads::shared">5.20.2</a> <a class="dropdown-item" href="/5.20.1/threads::shared">5.20.1</a> <a class="dropdown-item" href="/5.20.0/threads::shared">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/threads::shared">5.18.4</a> <a class="dropdown-item" href="/5.18.3/threads::shared">5.18.3</a> <a class="dropdown-item" href="/5.18.2/threads::shared">5.18.2</a> <a class="dropdown-item" href="/5.18.1/threads::shared">5.18.1</a> <a class="dropdown-item" href="/5.18.0/threads::shared">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/threads::shared">5.16.3</a> <a class="dropdown-item" href="/5.16.2/threads::shared">5.16.2</a> <a class="dropdown-item" href="/5.16.1/threads::shared">5.16.1</a> <a class="dropdown-item" href="/5.16.0/threads::shared">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/threads::shared">5.14.4</a> <a class="dropdown-item" href="/5.14.3/threads::shared">5.14.3</a> <a class="dropdown-item" href="/5.14.2/threads::shared">5.14.2</a> <a class="dropdown-item" href="/5.14.1/threads::shared">5.14.1</a> <a class="dropdown-item" href="/5.14.0/threads::shared">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/threads::shared">5.12.5</a> <a class="dropdown-item" href="/5.12.4/threads::shared">5.12.4</a> <a class="dropdown-item" href="/5.12.3/threads::shared">5.12.3</a> <a class="dropdown-item" href="/5.12.2/threads::shared">5.12.2</a> <a class="dropdown-item" href="/5.12.1/threads::shared">5.12.1</a> <a class="dropdown-item" href="/5.12.0/threads::shared">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/threads::shared">5.10.1</a> <a class="dropdown-item" href="/5.10.0/threads::shared">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/threads::shared">5.8.9</a> <a class="dropdown-item" href="/5.8.8/threads::shared">5.8.8</a> <a class="dropdown-item" href="/5.8.7/threads::shared">5.8.7</a> <a class="dropdown-item" href="/5.8.6/threads::shared">5.8.6</a> <a class="dropdown-item" href="/5.8.5/threads::shared">5.8.5</a> <a class="dropdown-item" href="/5.8.4/threads::shared">5.8.4</a> <a class="dropdown-item" href="/5.8.3/threads::shared">5.8.3</a> <a class="dropdown-item" href="/5.8.2/threads::shared">5.8.2</a> <a class="dropdown-item" href="/5.8.1/threads::shared">5.8.1</a> <a class="dropdown-item" href="/5.8.0/threads::shared">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/threads::shared">5.6.2</a> <a class="dropdown-item" href="/5.6.1/threads::shared">5.6.1</a> <a class="dropdown-item" href="/5.6.0/threads::shared">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/threads::shared">5.005_04</a> <a class="dropdown-item" href="/5.005_03/threads::shared">5.005_03</a> <a class="dropdown-item" href="/5.005_02/threads::shared">5.005_02</a> <a class="dropdown-item" href="/5.005_01/threads::shared">5.005_01</a> <a class="dropdown-item" href="/5.005/threads::shared">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/threads::shared">blead</a> <a class="dropdown-item" href="/5.41.10/threads::shared">5.41.10</a> <a class="dropdown-item" href="/5.41.9/threads::shared">5.41.9</a> <a class="dropdown-item" href="/5.41.8/threads::shared">5.41.8</a> <a class="dropdown-item" href="/5.41.7/threads::shared">5.41.7</a> <a class="dropdown-item" href="/5.41.6/threads::shared">5.41.6</a> <a class="dropdown-item" href="/5.41.5/threads::shared">5.41.5</a> <a class="dropdown-item" href="/5.41.4/threads::shared">5.41.4</a> <a class="dropdown-item" href="/5.41.3/threads::shared">5.41.3</a> <a class="dropdown-item" href="/5.41.2/threads::shared">5.41.2</a> <a class="dropdown-item" href="/5.41.1/threads::shared">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/threads::shared">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/threads::shared">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/threads::shared">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/threads::shared">5.39.10</a> <a class="dropdown-item" href="/5.39.9/threads::shared">5.39.9</a> <a class="dropdown-item" href="/5.39.8/threads::shared">5.39.8</a> <a class="dropdown-item" href="/5.39.7/threads::shared">5.39.7</a> <a class="dropdown-item" href="/5.39.6/threads::shared">5.39.6</a> <a class="dropdown-item" href="/5.39.5/threads::shared">5.39.5</a> <a class="dropdown-item" href="/5.39.4/threads::shared">5.39.4</a> <a class="dropdown-item" href="/5.39.3/threads::shared">5.39.3</a> <a class="dropdown-item" href="/5.39.2/threads::shared">5.39.2</a> <a class="dropdown-item" href="/5.39.1/threads::shared">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/threads::shared">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.28.3/perl">Perl</a> <a class="dropdown-item" href="/5.28.3/perlintro">Intro</a> <a class="dropdown-item" href="/5.28.3/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.28.3/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.28.3/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perlop">Operators</a> <a class="dropdown-item" href="/5.28.3/functions">Functions</a> <a class="dropdown-item" href="/5.28.3/variables">Variables</a> <a class="dropdown-item" href="/5.28.3/modules">Modules</a> <a class="dropdown-item" href="/5.28.3/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.28.3/perlcommunity">Community</a> <a class="dropdown-item" href="/5.28.3/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.28.3/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.28.3/threads::shared">threads::shared</a> <div id="more"> (<a href="/5.28.3/threads::shared.txt">source</a>, <a href="https://metacpan.org/pod/threads::shared">CPAN</a>) </div> <div id="moduleversion">version 1.58</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.28.3. <a href="/threads::shared">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#VERSION">VERSION</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#EXPORT">EXPORT</a> </li> <li> <a class="text-decoration-none" href="#FUNCTIONS">FUNCTIONS</a> </li> <li> <a class="text-decoration-none" href="#OBJECTS">OBJECTS</a> </li> <li> <a class="text-decoration-none" href="#NOTES">NOTES</a> </li> <li> <a class="text-decoration-none" href="#WARNINGS">WARNINGS</a> </li> <li> <a class="text-decoration-none" href="#BUGS-AND-LIMITATIONS">BUGS AND LIMITATIONS</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> <li> <a class="text-decoration-none" href="#LICENSE">LICENSE</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>threads::shared - Perl extension for sharing data structures between threads</p> <h1 id="VERSION"><a class="permalink" href="#VERSION">#</a>VERSION</h1> <p>This document describes threads::shared version 1.58</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use threads; use threads::shared; my $var :shared; my %hsh :shared; my @ary :shared; my ($scalar, @array, %hash); share($scalar); share(@array); share(%hash); $var = $scalar_value; $var = $shared_ref_value; $var = shared_clone($non_shared_ref_value); $var = shared_clone({&#39;foo&#39; =&gt; [qw/foo bar baz/]}); $hsh{&#39;foo&#39;} = $scalar_value; $hsh{&#39;bar&#39;} = $shared_ref_value; $hsh{&#39;baz&#39;} = shared_clone($non_shared_ref_value); $hsh{&#39;quz&#39;} = shared_clone([1..3]); $ary[0] = $scalar_value; $ary[1] = $shared_ref_value; $ary[2] = shared_clone($non_shared_ref_value); $ary[3] = shared_clone([ {}, [] ]); { lock(%hash); ... } cond_wait($scalar); cond_timedwait($scalar, time() + 30); cond_broadcast(@array); cond_signal(%hash); my $lockvar :shared; # condition var != lock var cond_wait($var, $lockvar); cond_timedwait($var, time()+30, $lockvar);</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>By default, variables are private to each thread, and each newly created thread gets a private copy of each existing variable. This module allows you to share variables across different threads (and pseudo-forks on Win32). It is used together with the <a href="/5.28.3/threads">threads</a> module.</p> <p>This module supports the sharing of the following data types only: scalars and scalar refs, arrays and array refs, and hashes and hash refs.</p> <h1 id="EXPORT"><a class="permalink" href="#EXPORT">#</a>EXPORT</h1> <p>The following functions are exported by this module: <code>share</code>, <code>shared_clone</code>, <code>is_shared</code>, <code>cond_wait</code>, <code>cond_timedwait</code>, <code>cond_signal</code> and <code>cond_broadcast</code></p> <p>Note that if this module is imported when <a href="/5.28.3/threads">threads</a> has not yet been loaded, then these functions all become no-ops. This makes it possible to write modules that will work in both threaded and non-threaded environments.</p> <h1 id="FUNCTIONS"><a class="permalink" href="#FUNCTIONS">#</a>FUNCTIONS</h1> <dl> <dt id="share-VARIABLE"><a class="permalink" href="#share-VARIABLE">#</a><a id="share"></a>share VARIABLE</dt> <dd> <p><code>share</code> takes a variable and marks it as shared:</p> <pre><code>my ($scalar, @array, %hash); share($scalar); share(@array); share(%hash);</code></pre> <p><code>share</code> will return the shared rvalue, but always as a reference.</p> <p>Variables can also be marked as shared at compile time by using the <code>:shared</code> attribute:</p> <pre><code>my ($var, %hash, @array) :shared;</code></pre> <p>Shared variables can only store scalars, refs of shared variables, or refs of shared data (discussed in next section):</p> <pre><code>my ($var, %hash, @array) :shared; my $bork; # Storing scalars $var = 1; $hash{&#39;foo&#39;} = &#39;bar&#39;; $array[0] = 1.5; # Storing shared refs $var = \%hash; $hash{&#39;ary&#39;} = \@array; $array[1] = \$var; # The following are errors: # $var = \$bork; # ref of non-shared variable # $hash{&#39;bork&#39;} = []; # non-shared array ref # push(@array, { &#39;x&#39; =&gt; 1 }); # non-shared hash ref</code></pre> </dd> <dt id="shared_clone-REF"><a class="permalink" href="#shared_clone-REF">#</a><a id="shared_clone"></a>shared_clone REF</dt> <dd> <p><code>shared_clone</code> takes a reference, and returns a shared version of its argument, performing a deep copy on any non-shared elements. Any shared elements in the argument are used as is (i.e., they are not cloned).</p> <pre><code>my $cpy = shared_clone({&#39;foo&#39; =&gt; [qw/foo bar baz/]});</code></pre> <p>Object status (i.e., the class an object is blessed into) is also cloned.</p> <pre><code>my $obj = {&#39;foo&#39; =&gt; [qw/foo bar baz/]}; bless($obj, &#39;Foo&#39;); my $cpy = shared_clone($obj); print(ref($cpy), &quot;\n&quot;); # Outputs &#39;Foo&#39;</code></pre> <p>For cloning empty array or hash refs, the following may also be used:</p> <pre><code>$var = &amp;share([]); # Same as $var = shared_clone([]); $var = &amp;share({}); # Same as $var = shared_clone({});</code></pre> <p>Not all Perl data types can be cloned (e.g., globs, code refs). By default, <code>shared_clone</code> will <a href="/5.28.3/Carp">croak</a> if it encounters such items. To change this behaviour to a warning, then set the following:</p> <pre><code>$threads::shared::clone_warn = 1;</code></pre> <p>In this case, <code>undef</code> will be substituted for the item to be cloned. If set to zero:</p> <pre><code>$threads::shared::clone_warn = 0;</code></pre> <p>then the <code>undef</code> substitution will be performed silently.</p> </dd> <dt id="is_shared-VARIABLE"><a class="permalink" href="#is_shared-VARIABLE">#</a><a id="is_shared"></a>is_shared VARIABLE</dt> <dd> <p><code>is_shared</code> checks if the specified variable is shared or not. If shared, returns the variable&#39;s internal ID (similar to <code>refaddr()</code> (see <a href="/5.28.3/Scalar::Util">Scalar::Util</a>). Otherwise, returns <code>undef</code>.</p> <pre><code>if (is_shared($var)) { print(&quot;\$var is shared\n&quot;); } else { print(&quot;\$var is not shared\n&quot;); }</code></pre> <p>When used on an element of an array or hash, <code>is_shared</code> checks if the specified element belongs to a shared array or hash. (It does not check the contents of that element.)</p> <pre><code>my %hash :shared; if (is_shared(%hash)) { print(&quot;\%hash is shared\n&quot;); } $hash{&#39;elem&#39;} = 1; if (is_shared($hash{&#39;elem&#39;})) { print(&quot;\$hash{&#39;elem&#39;} is in a shared hash\n&quot;); }</code></pre> </dd> <dt id="lock-VARIABLE"><a class="permalink" href="#lock-VARIABLE">#</a><a id="lock"></a>lock VARIABLE</dt> <dd> <p><code>lock</code> places a <b>advisory</b> lock on a variable until the lock goes out of scope. If the variable is locked by another thread, the <code>lock</code> call will block until it&#39;s available. Multiple calls to <code>lock</code> by the same thread from within dynamically nested scopes are safe -- the variable will remain locked until the outermost lock on the variable goes out of scope.</p> <p><code>lock</code> follows references exactly <i>one</i> level:</p> <pre><code>my %hash :shared; my $ref = \%hash; lock($ref); # This is equivalent to lock(%hash)</code></pre> <p>Note that you cannot explicitly unlock a variable; you can only wait for the lock to go out of scope. This is most easily accomplished by locking the variable inside a block.</p> <pre><code>my $var :shared; { lock($var); # $var is locked from here to the end of the block ... } # $var is now unlocked</code></pre> <p>As locks are advisory, they do not prevent data access or modification by another thread that does not itself attempt to obtain a lock on the variable.</p> <p>You cannot lock the individual elements of a container variable:</p> <pre><code>my %hash :shared; $hash{&#39;foo&#39;} = &#39;bar&#39;; #lock($hash{&#39;foo&#39;}); # Error lock(%hash); # Works</code></pre> <p>If you need more fine-grained control over shared variable access, see <a href="/5.28.3/Thread::Semaphore">Thread::Semaphore</a>.</p> </dd> <dt id="cond_wait-VARIABLE"><a class="permalink" href="#cond_wait-VARIABLE">#</a><a id="cond_wait"></a>cond_wait VARIABLE</dt> <dd> </dd> <dt id="cond_wait-CONDVAR,-LOCKVAR"><a class="permalink" href="#cond_wait-CONDVAR,-LOCKVAR">#</a><a id="cond_wait1"></a><a id="cond_wait-CONDVAR-LOCKVAR"></a>cond_wait CONDVAR, LOCKVAR</dt> <dd> <p>The <code>cond_wait</code> function takes a <b>locked</b> variable as a parameter, unlocks the variable, and blocks until another thread does a <code>cond_signal</code> or <code>cond_broadcast</code> for that same locked variable. The variable that <code>cond_wait</code> blocked on is re-locked after the <code>cond_wait</code> is satisfied. If there are multiple threads <code>cond_wait</code>ing on the same variable, all but one will re-block waiting to reacquire the lock on the variable. (So if you&#39;re only using <code>cond_wait</code> for synchronization, give up the lock as soon as possible). The two actions of unlocking the variable and entering the blocked wait state are atomic, the two actions of exiting from the blocked wait state and re-locking the variable are not.</p> <p>In its second form, <code>cond_wait</code> takes a shared, <b>unlocked</b> variable followed by a shared, <b>locked</b> variable. The second variable is unlocked and thread execution suspended until another thread signals the first variable.</p> <p>It is important to note that the variable can be notified even if no thread <code>cond_signal</code> or <code>cond_broadcast</code> on the variable. It is therefore important to check the value of the variable and go back to waiting if the requirement is not fulfilled. For example, to pause until a shared counter drops to zero:</p> <pre><code>{ lock($counter); cond_wait($counter) until $counter == 0; }</code></pre> </dd> <dt id="cond_timedwait-VARIABLE,-ABS_TIMEOUT"><a class="permalink" href="#cond_timedwait-VARIABLE,-ABS_TIMEOUT">#</a><a id="cond_timedwait"></a><a id="cond_timedwait-VARIABLE-ABS_TIMEOUT"></a>cond_timedwait VARIABLE, ABS_TIMEOUT</dt> <dd> </dd> <dt id="cond_timedwait-CONDVAR,-ABS_TIMEOUT,-LOCKVAR"><a class="permalink" href="#cond_timedwait-CONDVAR,-ABS_TIMEOUT,-LOCKVAR">#</a><a id="cond_timedwait1"></a><a id="cond_timedwait-CONDVAR-ABS_TIMEOUT-LOCKVAR"></a>cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR</dt> <dd> <p>In its two-argument form, <code>cond_timedwait</code> takes a <b>locked</b> variable and an absolute timeout in <i>epoch</i> seconds (see <a href="/5.28.3/perlfunc#time">time() in perlfunc</a> for more) as parameters, unlocks the variable, and blocks until the timeout is reached or another thread signals the variable. A false value is returned if the timeout is reached, and a true value otherwise. In either case, the variable is re-locked upon return.</p> <p>Like <code>cond_wait</code>, this function may take a shared, <b>locked</b> variable as an additional parameter; in this case the first parameter is an <b>unlocked</b> condition variable protected by a distinct lock variable.</p> <p>Again like <code>cond_wait</code>, waking up and reacquiring the lock are not atomic, and you should always check your desired condition after this function returns. Since the timeout is an absolute value, however, it does not have to be recalculated with each pass:</p> <pre><code>lock($var); my $abs = time() + 15; until ($ok = desired_condition($var)) { last if !cond_timedwait($var, $abs); } # we got it if $ok, otherwise we timed out!</code></pre> </dd> <dt id="cond_signal-VARIABLE"><a class="permalink" href="#cond_signal-VARIABLE">#</a><a id="cond_signal"></a>cond_signal VARIABLE</dt> <dd> <p>The <code>cond_signal</code> function takes a <b>locked</b> variable as a parameter and unblocks one thread that&#39;s <code>cond_wait</code>ing on that variable. If more than one thread is blocked in a <code>cond_wait</code> on that variable, only one (and which one is indeterminate) will be unblocked.</p> <p>If there are no threads blocked in a <code>cond_wait</code> on the variable, the signal is discarded. By always locking before signaling, you can (with care), avoid signaling before another thread has entered cond_wait().</p> <p><code>cond_signal</code> will normally generate a warning if you attempt to use it on an unlocked variable. On the rare occasions where doing this may be sensible, you can suppress the warning with:</p> <pre><code>{ no warnings &#39;threads&#39;; cond_signal($foo); }</code></pre> </dd> <dt id="cond_broadcast-VARIABLE"><a class="permalink" href="#cond_broadcast-VARIABLE">#</a><a id="cond_broadcast"></a>cond_broadcast VARIABLE</dt> <dd> <p>The <code>cond_broadcast</code> function works similarly to <code>cond_signal</code>. <code>cond_broadcast</code>, though, will unblock <b>all</b> the threads that are blocked in a <code>cond_wait</code> on the locked variable, rather than only one.</p> </dd> </dl> <h1 id="OBJECTS"><a class="permalink" href="#OBJECTS">#</a>OBJECTS</h1> <p><a href="/5.28.3/threads::shared">threads::shared</a> exports a version of <a href="/5.28.3/perlfunc#bless-REF">bless()</a> that works on shared objects such that <i>blessings</i> propagate across threads.</p> <pre><code># Create a shared &#39;Foo&#39; object my $foo :shared = shared_clone({}); bless($foo, &#39;Foo&#39;); # Create a shared &#39;Bar&#39; object my $bar :shared = shared_clone({}); bless($bar, &#39;Bar&#39;); # Put &#39;bar&#39; inside &#39;foo&#39; $foo-&gt;{&#39;bar&#39;} = $bar; # Rebless the objects via a thread threads-&gt;create(sub { # Rebless the outer object bless($foo, &#39;Yin&#39;); # Cannot directly rebless the inner object #bless($foo-&gt;{&#39;bar&#39;}, &#39;Yang&#39;); # Retrieve and rebless the inner object my $obj = $foo-&gt;{&#39;bar&#39;}; bless($obj, &#39;Yang&#39;); $foo-&gt;{&#39;bar&#39;} = $obj; })-&gt;join(); print(ref($foo), &quot;\n&quot;); # Prints &#39;Yin&#39; print(ref($foo-&gt;{&#39;bar&#39;}), &quot;\n&quot;); # Prints &#39;Yang&#39; print(ref($bar), &quot;\n&quot;); # Also prints &#39;Yang&#39;</code></pre> <h1 id="NOTES"><a class="permalink" href="#NOTES">#</a>NOTES</h1> <p><a href="/5.28.3/threads::shared">threads::shared</a> is designed to disable itself silently if threads are not available. This allows you to write modules and packages that can be used in both threaded and non-threaded applications.</p> <p>If you want access to threads, you must <code>use threads</code> before you <code>use threads::shared</code>. <a href="/5.28.3/threads">threads</a> will emit a warning if you use it after <a href="/5.28.3/threads::shared">threads::shared</a>.</p> <h1 id="WARNINGS"><a class="permalink" href="#WARNINGS">#</a>WARNINGS</h1> <dl> <dt id="cond_broadcast()-called-on-unlocked-variable"><a class="permalink" href="#cond_broadcast()-called-on-unlocked-variable">#</a><a id="cond_broadcast1"></a><a id="cond_broadcast-called-on-unlocked-variable"></a>cond_broadcast() called on unlocked variable</dt> <dd> </dd> <dt id="cond_signal()-called-on-unlocked-variable"><a class="permalink" href="#cond_signal()-called-on-unlocked-variable">#</a><a id="cond_signal1"></a><a id="cond_signal-called-on-unlocked-variable"></a>cond_signal() called on unlocked variable</dt> <dd> <p>See <a href="#cond_signal-VARIABLE">&quot;cond_signal VARIABLE&quot;</a>, above.</p> </dd> </dl> <h1 id="BUGS-AND-LIMITATIONS"><a class="permalink" href="#BUGS-AND-LIMITATIONS">#</a><a id="BUGS"></a>BUGS AND LIMITATIONS</h1> <p>When <code>share</code> is used on arrays, hashes, array refs or hash refs, any data they contain will be lost.</p> <pre><code>my @arr = qw(foo bar baz); share(@arr); # @arr is now empty (i.e., == ()); # Create a &#39;foo&#39; object my $foo = { &#39;data&#39; =&gt; 99 }; bless($foo, &#39;foo&#39;); # Share the object share($foo); # Contents are now wiped out print(&quot;ERROR: \$foo is empty\n&quot;) if (! exists($foo-&gt;{&#39;data&#39;}));</code></pre> <p>Therefore, populate such variables <b>after</b> declaring them as shared. (Scalar and scalar refs are not affected by this problem.)</p> <p>Blessing a shared item after it has been nested in another shared item does not propagate the blessing to the shared reference:</p> <pre><code>my $foo = &amp;share({}); my $bar = &amp;share({}); $bar-&gt;{foo} = $foo; bless($foo, &#39;baz&#39;); # $foo is now of class &#39;baz&#39;, # but $bar-&gt;{foo} is unblessed.</code></pre> <p>Therefore, you should bless objects before sharing them.</p> <p>It is often not wise to share an object unless the class itself has been written to support sharing. For example, a shared object&#39;s destructor may get called multiple times, once for each thread&#39;s scope exit, or may not get called at all if it is embedded inside another shared object. Another issue is that the contents of hash-based objects will be lost due to the above mentioned limitation. See <i>examples/class.pl</i> (in the CPAN distribution of this module) for how to create a class that supports object sharing.</p> <p>Destructors may not be called on objects if those objects still exist at global destruction time. If the destructors must be called, make sure there are no circular references and that nothing is referencing the objects before the program ends.</p> <p>Does not support <code>splice</code> on arrays. Does not support explicitly changing array lengths via $#array -- use <code>push</code> and <code>pop</code> instead.</p> <p>Taking references to the elements of shared arrays and hashes does not autovivify the elements, and neither does slicing a shared array/hash over non-existent indices/keys autovivify the elements.</p> <p><code>share()</code> allows you to <code>share($hashref-&gt;{key})</code> and <code>share($arrayref-&gt;[idx])</code> without giving any error message. But the <code>$hashref-&gt;{key}</code> or <code>$arrayref-&gt;[idx]</code> is <b>not</b> shared, causing the error &quot;lock can only be used on shared values&quot; to occur when you attempt to <code>lock($hashref-&gt;{key})</code> or <code>lock($arrayref-&gt;[idx])</code> in another thread.</p> <p>Using <code>refaddr()</code> is unreliable for testing whether or not two shared references are equivalent (e.g., when testing for circular references). Use <a href="#is_shared-VARIABLE">is_shared()</a>, instead:</p> <pre><code>use threads; use threads::shared; use Scalar::Util qw(refaddr); # If ref is shared, use threads::shared&#39;s internal ID. # Otherwise, use refaddr(). my $addr1 = is_shared($ref1) || refaddr($ref1); my $addr2 = is_shared($ref2) || refaddr($ref2); if ($addr1 == $addr2) { # The refs are equivalent }</code></pre> <p><a href="/5.28.3/perlfunc#each-HASH">each()</a> does not work properly on shared references embedded in shared structures. For example:</p> <pre><code>my %foo :shared; $foo{&#39;bar&#39;} = shared_clone({&#39;a&#39;=&gt;&#39;x&#39;, &#39;b&#39;=&gt;&#39;y&#39;, &#39;c&#39;=&gt;&#39;z&#39;}); while (my ($key, $val) = each(%{$foo{&#39;bar&#39;}})) { ... }</code></pre> <p>Either of the following will work instead:</p> <pre><code>my $ref = $foo{&#39;bar&#39;}; while (my ($key, $val) = each(%{$ref})) { ... } foreach my $key (keys(%{$foo{&#39;bar&#39;}})) { my $val = $foo{&#39;bar&#39;}{$key}; ... }</code></pre> <p>This module supports dual-valued variables created using <code>dualvar()</code> from <a href="/5.28.3/Scalar::Util">Scalar::Util</a>. However, while <code>$!</code> acts like a dualvar, it is implemented as a tied SV. To propagate its value, use the follow construct, if needed:</p> <pre><code>my $errno :shared = dualvar($!,$!);</code></pre> <p>View existing bug reports at, and submit any new bugs, problems, patches, etc. to: <a href="http://rt.cpan.org/Public/Dist/Display.html?Name=threads-shared">http://rt.cpan.org/Public/Dist/Display.html?Name=threads-shared</a></p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p>threads::shared on MetaCPAN: <a href="https://metacpan.org/release/threads-shared">https://metacpan.org/release/threads-shared</a></p> <p>Code repository for CPAN distribution: <a href="https://github.com/Dual-Life/threads-shared">https://github.com/Dual-Life/threads-shared</a></p> <p><a href="/5.28.3/threads">threads</a>, <a href="/5.28.3/perlthrtut">perlthrtut</a></p> <p><a href="http://www.perl.com/pub/a/2002/06/11/threads.html">http://www.perl.com/pub/a/2002/06/11/threads.html</a> and <a href="http://www.perl.com/pub/a/2002/09/04/threads.html">http://www.perl.com/pub/a/2002/09/04/threads.html</a></p> <p>Perl threads mailing list: <a href="http://lists.perl.org/list/ithreads.html">http://lists.perl.org/list/ithreads.html</a></p> <p>Sample code in the <i>examples</i> directory of this distribution on CPAN.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Artur Bergman &lt;sky AT crucially DOT net&gt;</p> <p>Documentation borrowed from the old Thread.pm.</p> <p>CPAN version produced by Jerry D. Hedden &lt;jdhedden AT cpan DOT org&gt;.</p> <h1 id="LICENSE"><a class="permalink" href="#LICENSE">#</a>LICENSE</h1> <p>threads::shared is released under the same license as Perl.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10