    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Const and Concurrency (part 2)</title>
        <link>https://members.accu.org/index.php/articles/2053</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 26, #6 - January 2015</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c345/">266</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-345/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+345/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Const and Concurrency (part 2)</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 05 January 2015 21:37:39 +00:00 or Mon, 05 January 2015 21:37:39 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Ralph McArdell continues musing on comments to Herb Sutterâ€™s updated GotW #6b solution.</p>

</p>
<p><strong>Body:</strong>&nbsp;<p>Previously <a href="#[Mc1]">[1]</a> I wondered, from musings when reading Herb Sutterâ€™s updated Guru of the Week 6b <a href="#[Mc2]">[2]</a> article, how one might â€“ in C++11 â€“ enforce a concurrent usage pattern in which an object can only be modified after creation by the creating thread until all modifications are done when the object becomes immutable and concurrently accessible. Concurrent access before an object becomes immutable is considered an error as are attempts to modify an object that is immutable.</p>

<p>Part 1 ended with a scheme in which erroneous updates in the mutable phase are detected by operations verifying they are called in the context of the creator thread by comparing the creator thread id â€“ an instance member â€“ with the callerâ€™s threadâ€™s id. I speculated that entering the immutable state could be indicated by setting the thread id member to the â€˜no executing threadâ€™ value of <code>std::thread::id</code> after which all non-mutating operations (spelt <code>const</code> in C++) may be called concurrently by any thread, and all calls to mutating operations would fail.</p>

<p>Before continuing I shall mention that I realise there are obvious, simpler, ways to arrange code to support this type of usage. That is not the point; the point is to see if such a usage pattern can be implemented in such a way as to report misuse and be convenient and efficient to boot while taking some of the new C++11 features out for a spin and seeing where we end up!</p>

<p>Letâ€™s continue by taking a detailed look at changing objects from mutable to immutable. This transition has two consequences:</p>

<ol>
	<li>The object cannot be modified at all other than to be destroyed. This could be termed <em>freezing</em> the object or similar.</li>
	<li>All threads may access the state of the object, thus all modifications need to be made visible to any reader threads. We might say this is <em>publishing</em> the object.</li>
</ol>

<p>On some hardware platforms we may be able to reliably achieve the first effect in the manner previously described â€“ that is by just setting the updating thread id member to â€˜no executing threadâ€™, but this ought to be an atomic update. If C++11â€™s <code>std::atomic</code> type template fully supported class-types, which it does not, we could just freeze an object something like so:</p>

<pre class="programlisting">
  void the_type::freeze()
  {
    validate_call_context();
    update_id.store(std::thread::id{},
       std::memory_order_relaxed);
    }</pre>

<p>The reasoning is thus: the only thread that can access the object initially is the creator thread â€“ all other threads will fail call context validation. After calling <code>freeze</code> even the creator thread would fail call context validation. Other threads will either see the original creator threadâ€™s id during call context validation or the updated â€˜no executing threadâ€™ value, neither of which will allow them to update the object.</p>

<p>Unfortunately no thread can even get read access to a frozen object as they will also in general be call context validated (the exception by the way would be for data that is initialised in the objectâ€™s constructor and never modified thereafter).  But after freezing non-mutating â€“ or <code>const</code> â€“ operations should be allowed. Providing an overloaded <code>const</code> qualified implementation of <code>validate_call_context</code> that allows access if the <code>update_id</code> is â€˜no executing threadâ€™ would achieve this.</p>

<p>This scheme will not fully publish an objectâ€™s state to other threads. To do this there needs to be inter-thread memory access synchronisation. Specifically, the creator-thread has to release all memory writes it has made and all other threads will have to ensure they acquire these released writes before reading their values. Of course, each reading thread should do this as efficiently as possible â€“ preferably only once.</p>

<p>Because in this case we have sets of pairwise synchronisation requirements between the creator thread and each reader thread acquire-release ordering can be used.</p>

<p>The obvious choice here would be to use a <code>std::atomic&lt;bool&gt;</code> flag that the creator thread store-releases to in the publishing operation and each reader thread load-acquires from:</p>

<pre class="programlisting">
  void the_type::publish()
  {
    validate_call_context();
    published.store(true,
                    std:: memory_order_release);
  }</pre>

<p>In which the published instance member is of type <code>std::atomic&lt;bool&gt;</code> initialised to <code>false</code>. This allows the two <code>validate_call_context</code> overloads to simply check published to see if the object has been published (see Listing 1).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
void the_type::validate_call_context()
{
  if ( published.load(std:: memory_order_acquire)
      || std::this_thread::get_id()!=update_id
     )
  {
    throw std::runtime_error
    { &quot;Illegal usage : Concurrent access or&quot;
      &quot;attempt at mutating operation on a&quot;
      &quot;published immutable object.&quot;
    };
  }
}

void the_type::validate_call_context() const
{
  if (!published.load(std:: memory_order_acquire)
      &amp;&amp; std::this_thread::get_id()!=update_id
     )
  {
    throw std::runtime_error
    { &quot;Illegal usage : Concurrent access to &quot;
      &quot; an unpublished object.&quot;
    };
  }
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>
 
<p>Note the difference in the checks for the <code>const</code> and non-<code>const</code> overloads.</p>
 
<p>Placing all the required scaffolding into a single class would allow â€˜clientâ€™ classes to provide the required support more conveniently. As it stands only <code>publish</code> needs to be accessed outside the typeâ€™s implementation so instances of such a support class could be included by composition as an instance member, with <code>publish</code> implemented as a forwarding operation to this member. Another possibility would be as a mix-in base class included by (private) inheritance.</p>
 
<p>There are still questions to resolve. Most prevalent is how the reader-threads know when they can access an object. With the scheme as discussed so far each reader thread would have to try a non-mutating operation repeatedly until it did not throw an exception â€“ which underlines that it would definitely be a Good Ideaâ„¢ to define a specific exception type in any real implementation.</p>

<p>Next, the scheme is intrusive â€“ each operation has to remember to do something to validate the call context such as calling <code>validate_call_context</code>. Not only that but each operation has to atomically fetch data with potential memory synchronisation overheads on <em>each</em> call.</p>

<p>In theory at least the memory synchronisation overheads could be reduced for those processors where such overheads are high â€“ namely those with a weakly ordered memory model â€“ by using <code>std::memory_order_consume</code>, which is intended to rely on data dependency ordering, in place of <code>std::memory_order_acquire</code> <a href="#[Mc3]">[3]</a>. As in this case all updates occur in the context of a single object, they would be dependent on the objectâ€™s <code>this</code> pointer. Thus we can replace the published flag with a <code>std::atomic&lt;T*&gt;</code>, where <code>T</code> is the type of our object, initialised to <code>nullptr</code> and store-released to a value of the objectâ€™s <code>this</code> pointer in <code>publish</code>.  The use of <code>std::memory_order_acquire</code> would be replaced by <code>std::memory_order_consume</code> in both <code>validate_call_context</code> overloads. Additionally, all references to the object would also have to be initially loaded via a call to <code>published.load(std:: memory_order_consume)</code> â€“ indicating that some refactoring of the code  might be in order.</p>

<p>Note that I said â€˜in theoryâ€™ in the preceding paragraph. This is because the current specification of C++11 makes it difficult for compiler writers to create an efficient, data-dependency ordering implement of <code>std::memory_order_consume</code> for weakly ordered CPUs and all implementations to date it appears take the lazy option of implementing <code>std::memory_order_consume</code> as <code>std::memory_order_acquire</code> <a href="#[Mc3]">[3]</a> <a href="#[Mc4]">[4]</a>.</p>

<p>The final problem that springs to mind is the question of knowing when it is safe to delete an object.</p>

<p>Then there is the question of what effect relaxing some of the constraints would have: allowing the transfer of update-status to another thread as mentioned towards the end of part 1 for example. So it seems the scheme is workable but there is definitely much room for improvement. I feel I may have to write up some further instalments at some pointâ€¦</p>

<h2>References</h2>

<p class="bibliomixed"><a id="[Mc1]"></a>[1]	Const and Concurrency (part 1), <em>CVu</em> Volume 26 Issue 5, November 2014</p>

<p class="bibliomixed"><a id="[Mc2]"></a>[2]	<a href="http://herbsutter.com/2013/05/28/gotw-6b-solution-const-correctness-part-2/">http://herbsutter.com/2013/05/28/gotw-6b-solution-const-correctness-part-2/</a></p>

<p class="bibliomixed"><a id="[Mc3]"></a>[3]	The Purpose of memory_order_consume in C++11,<a href="http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/">http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/</a></p>

<p class="bibliomixed"><a id="[Mc4]"></a>[4]	N4215: Towards Implementation and Use of memory_order_consume, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4215.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4215.pdf</a>. </p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
