You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jr...@apache.org on 2017/08/31 12:42:06 UTC

[27/36] qpid-site git commit: QPID-7903: Refresh the snapshot of the Proton C docs

http://git-wip-us.apache.org/repos/asf/qpid-site/blob/905cafdf/content/releases/qpid-proton-master/proton/cpp/api/multithreaded_client_8cpp-example.html
----------------------------------------------------------------------
diff --git a/content/releases/qpid-proton-master/proton/cpp/api/multithreaded_client_8cpp-example.html b/content/releases/qpid-proton-master/proton/cpp/api/multithreaded_client_8cpp-example.html
index bdc6fb5..c7ea5a7 100755
--- a/content/releases/qpid-proton-master/proton/cpp/api/multithreaded_client_8cpp-example.html
+++ b/content/releases/qpid-proton-master/proton/cpp/api/multithreaded_client_8cpp-example.html
@@ -94,7 +94,7 @@ $(document).ready(function(){initNavTree('multithreaded_client_8cpp-example.html
 </div><!--header-->
 <div class="contents">
 <p>A multithreaded sender and receiver.<b>Requires C++11</b></p>
-<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Licensed to the Apache Software Foundation (ASF) under one</span></div><div class="line"><span class="comment"> * or more contributor license agreements.  See the NOTICE file</span></div><div class="line"><span class="comment"> * distributed with this work for additional information</span></div><div class="line"><span class="comment"> * regarding copyright ownership.  The ASF licenses this file</span></div><div class="line"><span class="comment"> * to you under the Apache License, Version 2.0 (the</span></div><div class="line"><span class="comment"> * &quot;License&quot;); you may not use this file except in compliance</span></div><div class="line"><span class="comment"> * with the License.  You may obtain a copy of the License at</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *   http://www.apache.org/li
 censes/LICENSE-2.0</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * Unless required by applicable law or agreed to in writing,</span></div><div class="line"><span class="comment"> * software distributed under the License is distributed on an</span></div><div class="line"><span class="comment"> * &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span></div><div class="line"><span class="comment"> * KIND, either express or implied.  See the License for the</span></div><div class="line"><span class="comment"> * specific language governing permissions and limitations</span></div><div class="line"><span class="comment"> * under the License.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// C++11 only</span></div><div class="line"><span class="comment">//</span></div><div cla
 ss="line"><span class="comment">// A multi-threaded client that calls proton::container::run() in one thread, sends</span></div><div class="line"><span class="comment">// messages in another and receives messages in a third.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Note this client does not deal with flow-control. If the sender is faster</span></div><div class="line"><span class="comment">// than the receiver, messages will build up in memory on the sending side.</span></div><div class="line"><span class="comment">// See @ref multithreaded_client_flow_control.cpp for a more complex example with</span></div><div class="line"><span class="comment">// flow control.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// NOTE: no proper error handling</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="
 connection_8hpp.html">proton/connection.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="connection__options_8hpp.html">proton/connection_options.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="container_8hpp.html">proton/container.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="message_8hpp.html">proton/message.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="messaging__handler_8hpp.html">proton/messaging_handler.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="receiver_8hpp.html">proton/receiver.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="sender_8hpp.html">proton/sender.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt
 ;proton/work_queue.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;condition_variable&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;mutex&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;queue&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;sstream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// Lock output from threads to avoid scramblin</span></div><div class="line">std::mutex out_lock;</div><div class="line"><span class="preprocessor">#define OUT(x) do { std::lock_guard&lt;std::mutex&gt; l(out_lock); x; } while (false)</span></div><div class="line"></div><div class="line"><s
 pan class="comment">// Handler for a single thread-safe sending and receiving connection.</span></div><div class="line"><span class="keyword">class </span>client : <span class="keyword">public</span> <a name="_a0"></a><a class="code" href="classproton_1_1messaging__handler.html">proton::messaging_handler</a> {</div><div class="line">    <span class="comment">// Invariant</span></div><div class="line">    <span class="keyword">const</span> std::string url_;</div><div class="line">    <span class="keyword">const</span> std::string address_;</div><div class="line"></div><div class="line">    <span class="comment">// Only used in proton handler thread</span></div><div class="line">    <a name="_a1"></a><a class="code" href="classproton_1_1sender.html">proton::sender</a> sender_;</div><div class="line"></div><div class="line">    <span class="comment">// Shared by proton and user threads, protected by lock_</span></div><div class="line">    std::mutex lock_;</div><div class="line">    <a
  name="_a2"></a><a class="code" href="classproton_1_1work__queue.html">proton::work_queue</a> *work_queue_;</div><div class="line">    std::condition_variable sender_ready_;</div><div class="line">    std::queue&lt;proton::message&gt; messages_;</div><div class="line">    std::condition_variable messages_ready_;</div><div class="line"></div><div class="line">  <span class="keyword">public</span>:</div><div class="line">    client(<span class="keyword">const</span> std::string&amp; url, <span class="keyword">const</span> std::string&amp; address) : url_(url), address_(address), work_queue_(0) {}</div><div class="line"></div><div class="line">    <span class="comment">// Thread safe</span></div><div class="line">    <span class="keywordtype">void</span> send(<span class="keyword">const</span> <a name="_a3"></a><a class="code" href="classproton_1_1message.html">proton::message</a>&amp; msg) {</div><div class="line">        <span class="comment">// Use [=] to copy the message, we cannot
  pass it by reference since it</span></div><div class="line">        <span class="comment">// will be used in another thread.</span></div><div class="line">        work_queue()-&gt;<a name="a4"></a><a class="code" href="classproton_1_1work__queue.html#a5a43f2afe9f74bb9281211a22f291140">add</a>([=]() { sender_.<a name="a5"></a><a class="code" href="classproton_1_1sender.html#a214eb30b24e6831d016a47b9dddda830">send</a>(msg); });</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Thread safe</span></div><div class="line">    <a class="code" href="classproton_1_1message.html">proton::message</a> receive() {</div><div class="line">        std::unique_lock&lt;std::mutex&gt; l(lock_);</div><div class="line">        <span class="keywordflow">while</span> (messages_.empty()) messages_ready_.wait(l);</div><div class="line">        <span class="keyword">auto</span> msg = std::move(messages_.front());</div><div class="line">        messages_
 .pop();</div><div class="line">        <span class="keywordflow">return</span> msg;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Thread safe</span></div><div class="line">    <span class="keywordtype">void</span> close() {</div><div class="line">        work_queue()-&gt;add([=]() { sender_.<a name="a6"></a><a class="code" href="classproton_1_1link.html#aff302bb6016f2ae29f01bb4e07389a52">connection</a>().<a name="a7"></a><a class="code" href="classproton_1_1connection.html#a5ae591df94fc66ccb85cbb6565368bca">close</a>(); });</div><div class="line">    }</div><div class="line"></div><div class="line">  <span class="keyword">private</span>:</div><div class="line"></div><div class="line">    <a class="code" href="classproton_1_1work__queue.html">proton::work_queue</a>* work_queue() {</div><div class="line">        <span class="comment">// Wait till work_queue_ and sender_ are initialized.</span></div><div class="line">        st
 d::unique_lock&lt;std::mutex&gt; l(lock_);</div><div class="line">        <span class="keywordflow">while</span> (!work_queue_) sender_ready_.wait(l);</div><div class="line">        <span class="keywordflow">return</span> work_queue_;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// == messaging_handler overrides, only called in proton hander thread</span></div><div class="line"></div><div class="line">    <span class="comment">// Note: this example creates a connection when the container starts.</span></div><div class="line">    <span class="comment">// To create connections after the container has started, use</span></div><div class="line">    <span class="comment">// container::connect().</span></div><div class="line">    <span class="comment">// See @ref multithreaded_client_flow_control.cpp for an example.</span></div><div class="line">    <span class="keywordtype">void</span> <a name="a8"></a><a class="code" href="classpr
 oton_1_1messaging__handler.html#a4949b0c93402fae0c07492d984688a25">on_container_start</a>(<a name="_a9"></a><a class="code" href="classproton_1_1container.html">proton::container</a>&amp; cont)<span class="keyword"> override </span>{</div><div class="line">        cont.<a name="a10"></a><a class="code" href="classproton_1_1container.html#afd13bc7f337ccd7029f5eb22a758fe59">connect</a>(url_);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a11"></a><a class="code" href="classproton_1_1messaging__handler.html#a16ba022e899957b2e25b9561b1f53968">on_connection_open</a>(<a name="_a12"></a><a class="code" href="classproton_1_1connection.html">proton::connection</a>&amp; conn)<span class="keyword"> override </span>{</div><div class="line">        conn.<a name="a13"></a><a class="code" href="classproton_1_1connection.html#ae8eece4fd4b9e1a2531ca12d2ab57a32">open_sender</a>(address_);</div><div class="line">        c
 onn.<a name="a14"></a><a class="code" href="classproton_1_1connection.html#aad60d14592ee9d34caca4c61214ecd27">open_receiver</a>(address_);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a15"></a><a class="code" href="classproton_1_1messaging__handler.html#a0b5d066e5463d3365f662c8a7dc52661">on_sender_open</a>(<a class="code" href="classproton_1_1sender.html">proton::sender</a>&amp; s)<span class="keyword"> override </span>{</div><div class="line">        <span class="comment">// sender_ and work_queue_ must be set atomically</span></div><div class="line">        std::lock_guard&lt;std::mutex&gt; l(lock_);</div><div class="line">        sender_ = s;</div><div class="line">        work_queue_ = &amp;s.<a name="a16"></a><a class="code" href="classproton_1_1link.html#a7c755d6ac6385e007adb61966598ba63">work_queue</a>();</div><div class="line">        sender_ready_.notify_all();</div><div class="line">    }</di
 v><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a17"></a><a class="code" href="classproton_1_1messaging__handler.html#a96879b0b0b7ade11f98d3f450abd4c46">on_message</a>(<a name="_a18"></a><a class="code" href="classproton_1_1delivery.html">proton::delivery</a>&amp; dlv, <a class="code" href="classproton_1_1message.html">proton::message</a>&amp; msg)<span class="keyword"> override </span>{</div><div class="line">        std::lock_guard&lt;std::mutex&gt; l(lock_);</div><div class="line">        messages_.push(msg);</div><div class="line">        messages_ready_.notify_all();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a19"></a><a class="code" href="classproton_1_1messaging__handler.html#a5e29fef3b8c5b8cf18aa7d69850ac22e">on_error</a>(<span class="keyword">const</span> <a name="_a20"></a><a class="code" href="classproton_1_1error__condition.html">proton::erro
 r_condition</a>&amp; e)<span class="keyword"> override </span>{</div><div class="line">        OUT(std::cerr &lt;&lt; <span class="stringliteral">&quot;unexpected error: &quot;</span> &lt;&lt; e &lt;&lt; std::endl);</div><div class="line">        exit(1);</div><div class="line">    }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span>** argv) {</div><div class="line">    <span class="keywordflow">try</span> {</div><div class="line">        <span class="keywordflow">if</span> (argc != 4) {</div><div class="line">            std ::cerr &lt;&lt;</div><div class="line">                <span class="stringliteral">&quot;Usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; CONNECTION-URL AMQP-ADDRESS MESSAGE-COUNT\n&quot;</span></div><div class="line">                <span class="strin
 gliteral">&quot;CONNECTION-URL: connection address, e.g.&#39;amqp://127.0.0.1&#39;\n&quot;</span></div><div class="line">                <span class="stringliteral">&quot;AMQP-ADDRESS: AMQP node address, e.g. &#39;examples&#39;\n&quot;</span></div><div class="line">                <span class="stringliteral">&quot;MESSAGE-COUNT: number of messages to send\n&quot;</span>;</div><div class="line">            <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">char</span> *url = argv[1];</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">char</span> *address = argv[2];</div><div class="line">        <span class="keywordtype">int</span> n_messages = atoi(argv[3]);</div><div class="line"></div><div class="line">        client cl(url, address);</div><div class="line">        <a class="code" href="classproton_1_1container.html">proton::
 container</a> container(cl);</div><div class="line">        std::thread container_thread([&amp;]() { container.<a name="a21"></a><a class="code" href="classproton_1_1container.html#a13a43e6d814de94978c515cb084873b1">run</a>(); });</div><div class="line"></div><div class="line">        std::thread sender([&amp;]() {</div><div class="line">                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n_messages; ++i) {</div><div class="line">                    <a class="code" href="classproton_1_1message.html">proton::message</a> msg(<a name="a22"></a><a class="code" href="namespaceproton.html#a1367d11b0ac6a99ea8e7d9a5b8f36b34">std::to_string</a>(i + 1));</div><div class="line">                    cl.send(msg);</div><div class="line">                    OUT(std::cout &lt;&lt; <span class="stringliteral">&quot;sent: &quot;</span> &lt;&lt; msg.<a name="a23"></a><a class="code" href="classproton_1_1message.html#ae9af642f154a68ec0eb8e715ecaf95ae
 ">body</a>() &lt;&lt; std::endl);</div><div class="line">                }</div><div class="line">            });</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> received = 0;</div><div class="line">        std::thread receiver([&amp;]() {</div><div class="line">                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n_messages; ++i) {</div><div class="line">                    <span class="keyword">auto</span> msg = cl.receive();</div><div class="line">                    OUT(std::cout &lt;&lt; <span class="stringliteral">&quot; received: &quot;</span> &lt;&lt; msg.body() &lt;&lt; std::endl);</div><div class="line">                    ++received;</div><div class="line">                }</div><div class="line">            });</div><div class="line"></div><div class="line">        sender.join();</div><div class="line">        receiver.join();</div><div class="line">        cl.close();</div><d
 iv class="line">        container_thread.join();</div><div class="line">        std::cout &lt;&lt; received &lt;&lt; <span class="stringliteral">&quot; messages sent and received&quot;</span> &lt;&lt; std::endl;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::exception&amp; e) {</div><div class="line">        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
+<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Licensed to the Apache Software Foundation (ASF) under one</span></div><div class="line"><span class="comment"> * or more contributor license agreements.  See the NOTICE file</span></div><div class="line"><span class="comment"> * distributed with this work for additional information</span></div><div class="line"><span class="comment"> * regarding copyright ownership.  The ASF licenses this file</span></div><div class="line"><span class="comment"> * to you under the Apache License, Version 2.0 (the</span></div><div class="line"><span class="comment"> * &quot;License&quot;); you may not use this file except in compliance</span></div><div class="line"><span class="comment"> * with the License.  You may obtain a copy of the License at</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *   http://www.apache.org/li
 censes/LICENSE-2.0</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * Unless required by applicable law or agreed to in writing,</span></div><div class="line"><span class="comment"> * software distributed under the License is distributed on an</span></div><div class="line"><span class="comment"> * &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span></div><div class="line"><span class="comment"> * KIND, either express or implied.  See the License for the</span></div><div class="line"><span class="comment"> * specific language governing permissions and limitations</span></div><div class="line"><span class="comment"> * under the License.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// C++11 only</span></div><div class="line"><span class="comment">//</span></div><div cla
 ss="line"><span class="comment">// A multi-threaded client that calls proton::container::run() in one thread, sends</span></div><div class="line"><span class="comment">// messages in another and receives messages in a third.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Note this client does not deal with flow-control. If the sender is faster</span></div><div class="line"><span class="comment">// than the receiver, messages will build up in memory on the sending side.</span></div><div class="line"><span class="comment">// See @ref multithreaded_client_flow_control.cpp for a more complex example with</span></div><div class="line"><span class="comment">// flow control.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// NOTE: no proper error handling</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="
 connection_8hpp.html">proton/connection.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="connection__options_8hpp.html">proton/connection_options.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="container_8hpp.html">proton/container.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="message_8hpp.html">proton/message.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="messaging__handler_8hpp.html">proton/messaging_handler.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="receiver_8hpp.html">proton/receiver.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="sender_8hpp.html">proton/sender.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt
 ;proton/work_queue.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;condition_variable&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;mutex&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;queue&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;sstream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// Lock output from threads to avoid scramblin</span></div><div class="line">std::mutex out_lock;</div><div class="line"><span class="preprocessor">#define OUT(x) do { std::lock_guard&lt;std::mutex&gt; l(out_lock); x; } while (false)</span></div><div class="line"></div><div class="line"><s
 pan class="comment">// Handler for a single thread-safe sending and receiving connection.</span></div><div class="line"><span class="keyword">class </span>client : <span class="keyword">public</span> <a name="_a0"></a><a class="code" href="classproton_1_1messaging__handler.html">proton::messaging_handler</a> {</div><div class="line">    <span class="comment">// Invariant</span></div><div class="line">    <span class="keyword">const</span> std::string url_;</div><div class="line">    <span class="keyword">const</span> std::string address_;</div><div class="line"></div><div class="line">    <span class="comment">// Only used in proton handler thread</span></div><div class="line">    <a name="_a1"></a><a class="code" href="classproton_1_1sender.html">proton::sender</a> sender_;</div><div class="line"></div><div class="line">    <span class="comment">// Shared by proton and user threads, protected by lock_</span></div><div class="line">    std::mutex lock_;</div><div class="line">    <a
  name="_a2"></a><a class="code" href="classproton_1_1work__queue.html">proton::work_queue</a> *work_queue_;</div><div class="line">    std::condition_variable sender_ready_;</div><div class="line">    std::queue&lt;proton::message&gt; messages_;</div><div class="line">    std::condition_variable messages_ready_;</div><div class="line"></div><div class="line">  <span class="keyword">public</span>:</div><div class="line">    client(<span class="keyword">const</span> std::string&amp; url, <span class="keyword">const</span> std::string&amp; address) : url_(url), address_(address), work_queue_(0) {}</div><div class="line"></div><div class="line">    <span class="comment">// Thread safe</span></div><div class="line">    <span class="keywordtype">void</span> send(<span class="keyword">const</span> <a name="_a3"></a><a class="code" href="classproton_1_1message.html">proton::message</a>&amp; msg) {</div><div class="line">        <span class="comment">// Use [=] to copy the message, we cannot
  pass it by reference since it</span></div><div class="line">        <span class="comment">// will be used in another thread.</span></div><div class="line">        work_queue()-&gt;<a name="a4"></a><a class="code" href="classproton_1_1work__queue.html#a5a43f2afe9f74bb9281211a22f291140">add</a>([=]() { sender_.<a name="a5"></a><a class="code" href="classproton_1_1sender.html#a214eb30b24e6831d016a47b9dddda830">send</a>(msg); });</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Thread safe</span></div><div class="line">    <a class="code" href="classproton_1_1message.html">proton::message</a> receive() {</div><div class="line">        std::unique_lock&lt;std::mutex&gt; l(lock_);</div><div class="line">        <span class="keywordflow">while</span> (messages_.empty()) messages_ready_.wait(l);</div><div class="line">        <span class="keyword">auto</span> msg = std::move(messages_.front());</div><div class="line">        messages_
 .pop();</div><div class="line">        <span class="keywordflow">return</span> msg;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Thread safe</span></div><div class="line">    <span class="keywordtype">void</span> close() {</div><div class="line">        work_queue()-&gt;add([=]() { sender_.<a name="a6"></a><a class="code" href="classproton_1_1link.html#aff302bb6016f2ae29f01bb4e07389a52">connection</a>().<a name="a7"></a><a class="code" href="classproton_1_1connection.html#a5ae591df94fc66ccb85cbb6565368bca">close</a>(); });</div><div class="line">    }</div><div class="line"></div><div class="line">  <span class="keyword">private</span>:</div><div class="line"></div><div class="line">    <a class="code" href="classproton_1_1work__queue.html">proton::work_queue</a>* work_queue() {</div><div class="line">        <span class="comment">// Wait till work_queue_ and sender_ are initialized.</span></div><div class="line">        st
 d::unique_lock&lt;std::mutex&gt; l(lock_);</div><div class="line">        <span class="keywordflow">while</span> (!work_queue_) sender_ready_.wait(l);</div><div class="line">        <span class="keywordflow">return</span> work_queue_;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// == messaging_handler overrides, only called in proton hander thread</span></div><div class="line"></div><div class="line">    <span class="comment">// Note: this example creates a connection when the container starts.</span></div><div class="line">    <span class="comment">// To create connections after the container has started, use</span></div><div class="line">    <span class="comment">// container::connect().</span></div><div class="line">    <span class="comment">// See @ref multithreaded_client_flow_control.cpp for an example.</span></div><div class="line">    <span class="keywordtype">void</span> <a name="a8"></a><a class="code" href="classpr
 oton_1_1messaging__handler.html#a4949b0c93402fae0c07492d984688a25">on_container_start</a>(<a name="_a9"></a><a class="code" href="classproton_1_1container.html">proton::container</a>&amp; cont)<span class="keyword"> override </span>{</div><div class="line">        cont.<a name="a10"></a><a class="code" href="classproton_1_1container.html#afd13bc7f337ccd7029f5eb22a758fe59">connect</a>(url_);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a11"></a><a class="code" href="classproton_1_1messaging__handler.html#a16ba022e899957b2e25b9561b1f53968">on_connection_open</a>(<a name="_a12"></a><a class="code" href="classproton_1_1connection.html">proton::connection</a>&amp; conn)<span class="keyword"> override </span>{</div><div class="line">        conn.<a name="a13"></a><a class="code" href="classproton_1_1connection.html#ae8eece4fd4b9e1a2531ca12d2ab57a32">open_sender</a>(address_);</div><div class="line">        c
 onn.<a name="a14"></a><a class="code" href="classproton_1_1connection.html#aad60d14592ee9d34caca4c61214ecd27">open_receiver</a>(address_);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a15"></a><a class="code" href="classproton_1_1messaging__handler.html#a0b5d066e5463d3365f662c8a7dc52661">on_sender_open</a>(<a class="code" href="classproton_1_1sender.html">proton::sender</a>&amp; s)<span class="keyword"> override </span>{</div><div class="line">        <span class="comment">// sender_ and work_queue_ must be set atomically</span></div><div class="line">        std::lock_guard&lt;std::mutex&gt; l(lock_);</div><div class="line">        sender_ = s;</div><div class="line">        work_queue_ = &amp;s.<a name="a16"></a><a class="code" href="classproton_1_1link.html#a7c755d6ac6385e007adb61966598ba63">work_queue</a>();</div><div class="line">        sender_ready_.notify_all();</div><div class="line">    }</di
 v><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a17"></a><a class="code" href="classproton_1_1messaging__handler.html#a96879b0b0b7ade11f98d3f450abd4c46">on_message</a>(<a name="_a18"></a><a class="code" href="classproton_1_1delivery.html">proton::delivery</a>&amp; dlv, <a class="code" href="classproton_1_1message.html">proton::message</a>&amp; msg)<span class="keyword"> override </span>{</div><div class="line">        std::lock_guard&lt;std::mutex&gt; l(lock_);</div><div class="line">        messages_.push(msg);</div><div class="line">        messages_ready_.notify_all();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a name="a19"></a><a class="code" href="classproton_1_1messaging__handler.html#a5e29fef3b8c5b8cf18aa7d69850ac22e">on_error</a>(<span class="keyword">const</span> <a name="_a20"></a><a class="code" href="classproton_1_1error__condition.html">proton::erro
 r_condition</a>&amp; e)<span class="keyword"> override </span>{</div><div class="line">        OUT(std::cerr &lt;&lt; <span class="stringliteral">&quot;unexpected error: &quot;</span> &lt;&lt; e &lt;&lt; std::endl);</div><div class="line">        exit(1);</div><div class="line">    }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span>** argv) {</div><div class="line">    <span class="keywordflow">try</span> {</div><div class="line">        <span class="keywordflow">if</span> (argc != 4) {</div><div class="line">            std ::cerr &lt;&lt;</div><div class="line">                <span class="stringliteral">&quot;Usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; CONNECTION-URL AMQP-ADDRESS MESSAGE-COUNT\n&quot;</span></div><div class="line">                <span class="strin
 gliteral">&quot;CONNECTION-URL: connection address, e.g.&#39;amqp://127.0.0.1&#39;\n&quot;</span></div><div class="line">                <span class="stringliteral">&quot;AMQP-ADDRESS: AMQP node address, e.g. &#39;examples&#39;\n&quot;</span></div><div class="line">                <span class="stringliteral">&quot;MESSAGE-COUNT: number of messages to send\n&quot;</span>;</div><div class="line">            <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">char</span> *url = argv[1];</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">char</span> *address = argv[2];</div><div class="line">        <span class="keywordtype">int</span> n_messages = atoi(argv[3]);</div><div class="line"></div><div class="line">        client cl(url, address);</div><div class="line">        <a class="code" href="classproton_1_1container.html">proton::
 container</a> container(cl);</div><div class="line">        std::thread container_thread([&amp;]() { container.<a name="a21"></a><a class="code" href="classproton_1_1container.html#a13a43e6d814de94978c515cb084873b1">run</a>(); });</div><div class="line"></div><div class="line">        std::thread sender([&amp;]() {</div><div class="line">                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n_messages; ++i) {</div><div class="line">                    <a class="code" href="classproton_1_1message.html">proton::message</a> msg(<a name="a22"></a><a class="code" href="namespaceproton.html#a1367d11b0ac6a99ea8e7d9a5b8f36b34">std::to_string</a>(i + 1));</div><div class="line">                    cl.send(msg);</div><div class="line">                    OUT(std::cout &lt;&lt; <span class="stringliteral">&quot;sent \&quot;&quot;</span> &lt;&lt; msg.<a name="a23"></a><a class="code" href="classproton_1_1message.html#ae9af642f154a68ec0eb8e715ec
 af95ae">body</a>() &lt;&lt; <span class="charliteral">&#39;&quot;&#39;</span> &lt;&lt; std::endl);</div><div class="line">                }</div><div class="line">            });</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> received = 0;</div><div class="line">        std::thread receiver([&amp;]() {</div><div class="line">                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n_messages; ++i) {</div><div class="line">                    <span class="keyword">auto</span> msg = cl.receive();</div><div class="line">                    OUT(std::cout &lt;&lt; <span class="stringliteral">&quot;received \&quot;&quot;</span> &lt;&lt; msg.body() &lt;&lt; <span class="charliteral">&#39;&quot;&#39;</span> &lt;&lt; std::endl);</div><div class="line">                    ++received;</div><div class="line">                }</div><div class="line">            });</div><div class="line"></div><div class
 ="line">        sender.join();</div><div class="line">        receiver.join();</div><div class="line">        cl.close();</div><div class="line">        container_thread.join();</div><div class="line">        std::cout &lt;&lt; received &lt;&lt; <span class="stringliteral">&quot; messages sent and received&quot;</span> &lt;&lt; std::endl;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::exception&amp; e) {</div><div class="line">        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
 </div><!-- doc-content -->
 <!-- start footer part -->
 <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org