<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>concurrency on autosys</title>
    <link>https://autosys.informatik.haw-hamburg.de/tags/concurrency/</link>
    <description>Recent content in concurrency on autosys</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator>
    <language>en-us</language>
    <copyright>&amp;copy; {year}</copyright>
    <lastBuildDate>Mon, 13 Feb 2017 00:00:00 +0000</lastBuildDate>
    
	    <atom:link href="https://autosys.informatik.haw-hamburg.de/tags/concurrency/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Fully Synchronous</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/fully-synchronous-buffer/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/fully-synchronous-buffer/</guid>
      <description>&lt;p&gt;A buffer is used for communication between objects which run in different threads. A buffer wraps some shared memory or a unique pointer thereto. The buffer may be accessed by the two communication partners via reference to the buffer which is initialized via constructor argument.&lt;/p&gt;
&lt;p&gt;A buffer is a single block of memory for a message (or unique pointer) and messages are not queued. Check out the channel if you need communication via a message queue. There are different ways to use a buffer for communication. Either it is ensured that the producer waits until the latest message has been read by the consumer, and the consumer waits until a new message has been provided by the producer. This is called fully synchronous buffer (fsb). No messages are lost or repeatedly read.&lt;/p&gt;
&lt;p&gt;Another possibility is the contiuous write buffer, where the producer does not wait for the consumer to read and  overwrites messages. Messages may be lost in this case if the consumer read slower than the producer writes. The contiuous asynchronous buffer (cab) does not wait on either side. The consumer may read the same message if no new messages are provided by the producer.&lt;/p&gt;
&lt;p&gt;This code example shows how the full_sync_buffer is used. Three thread objects are created (producer, broker, consumer). A message is created in the producer, which is  passed on to the broker which passes it on to the consumer. Two buffers are used to realize this (lines 108 and 109), which are given to the thread objects via constructor argument (lines 110 - 112). Broker and Consumer block on the get-call and wait for the message to arrive. The Producer blocks on the put-call until the receiver is ready to receive (i.e. until the buffer is empty). This way the communication is synchronized.&lt;/p&gt;
&lt;p&gt;Once the broker has passed on the message, the producer could go on and send the next message to the broker (not shown in the code) while the consumer might still be busy with the first message. The producer needs to create new memory for each message. A unique pointer to the message is passed on between the communication partners. Be aware at this point, that the new call will take a lot of time if used frequently. It is a good idea to use a memory pool at this place.&lt;/p&gt;
&lt;p&gt;The memory of the message is automatically cleaned up by the use of a unique pointer. This happens in the moment the unique pointer in the consumer is overwritten by the next message.&lt;/p&gt;
&lt;br /&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/stephanpareigis/29716e3747af945766877e1adcd3891d.js&#34;&gt;&lt;/script&gt;

</description>
    </item>
    
    <item>
      <title>Semaphore</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/semaphore/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/semaphore/</guid>
      <description>&lt;p&gt;Object-oriented semaphores are not part of the C++ standard at this point. However, it is possible to implement a semaphore class using a mutex and a condition variable both of which where added to the standard in C++11.&lt;/p&gt;
&lt;p&gt;For the implementation shown on this website the function names where derived from the POSIX semaphores. The post function increases the count of the semaphore and notifies a thread that might be waiting on the semaphore if its count is at zero. The wait function decreases the semaphore count and blocks if the count reaches zero. Any thread calling wait when the count is at zero waits on the condition variable until notified that the count has increased.&lt;/p&gt;
&lt;p&gt;A variation on this function is the try_wait function which does not block but simply return its success in acquiring the semaphore in the form of a bool.&lt;/p&gt;
&lt;br /&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/stephanpareigis/ba323685188700537179922ff283cd52.js&#34;&gt;&lt;/script&gt;

</description>
    </item>
    
    <item>
      <title>Thread Control</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/thread-control/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/thread-control/</guid>
      <description>&lt;h1 id=&#34;how-to-stop-a-thread&#34;&gt;How to stop a thread.&lt;/h1&gt;
&lt;p&gt;It is a nice idea to create a functor (overload operator()) and pass a functor object to a thread. The operator()() (line 9) should generally contain a while loop (event loop)(line 10). To be able to stop the while loop from the outside, one can use a member variable of the functor, e.g. bool IsStopped or bool IsRunning (line 15). The loop would then look like this&lt;/p&gt;
&lt;p&gt;&lt;code&gt;while(IsRunning){...}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Another thread (e.g. the main thread) could then set the flag to true (or false) (line 23) and this will end the while loop.&lt;/p&gt;
&lt;p&gt;In order to make this work, the functor object has to be passed as a reference to the thread (line 20).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;thread(std::ref(thread01));&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If you need more sophisticated communication between threads, you should use the buffer or channel which are described in separate posts.&lt;/p&gt;
&lt;br /&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/stephanpareigis/09a26bc9e7b391bd6f29532c198f1b99.js&#34;&gt;&lt;/script&gt;

</description>
    </item>
    
  </channel>
</rss>
