<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>utility on autosys</title>
    <link>https://autosys.informatik.haw-hamburg.de/categories/utility/</link>
    <description>Recent content in utility 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/categories/utility/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Buffer</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/buffer/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/buffer/</guid>
      <description>&lt;p&gt;Uncoupled communication between two objects which run in separate threads can be achieved through buffers. A buffer is placed between the two communicating objects and can store a single element. There are different types of buffers depending on the context in which they are used but they all share the same interface which supplies two functions: put and get. Three different variations have to be considered:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;fully synchronous: put and get both block the calling thread. The put call blocks when there is data already present in the buffer and get blocks when called on an empty buffer. Calling get empties the buffer.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;continuous write: get blocks on an empty buffer, put doesn&amp;rsquo;t block but overwrites the current data (if present) in the buffer. The get call moves the data out of the buffer, meaning that all data is only read once.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;continuous read: the buffer is initialised with a default value. Neither put nor get block. The data in the buffer can be re-read any number of times and can always be overwritten.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The fully synchronous and continuous write buffers both store a unique element which is removed from the buffer as soon as get is called. Because of this the internal storage is implemented with a &lt;code&gt;std::unique_ptr&lt;/code&gt;. For the continuous read buffer this is not the case so it just stores the element by value because the buffer is never empty.&lt;/p&gt;
&lt;br /&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/tillKaiser/62e6bcfe773c31040a14b181a28782f6.js&#34;&gt;&lt;/script&gt;

&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/tillKaiser/5cf9e1d9b10818086d71749645b40693.js&#34;&gt;&lt;/script&gt;

&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/tillKaiser/4af564d50aae7e6576ea239ba5ec0612.js&#34;&gt;&lt;/script&gt;

</description>
    </item>
    
    <item>
      <title>Channel</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/channel/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/channel/</guid>
      <description>&lt;p&gt;A channel is a thread-safe queue which uses semaphores to synchronize access. The underlying queue in this implementation is the C++ std::queue container. It uses two semaphores sem_free_spaces_ to count the free spaces in the queue and avoid overflow and sem_size_ to count the elements in the queue and prevent underflow. In addition a std::mutex is used to synchronize access to the queue.&lt;/p&gt;
&lt;p&gt;To enqueue an element to the channel the sem_free_spaces_ semaphore is first  decremented which will block on a full queue and then increments the sem_size_ semaphore after adding the element to the queue.&lt;/p&gt;
&lt;p&gt;Dequeuing works the opposite way by first decrementing sem_size_ to block on an empty queue.&lt;/p&gt;
&lt;p&gt;In addition the &amp;laquo; operator has been overloaded to move items into the channel or out of it. It also supports moving an item directly from one channel to another.&lt;/p&gt;
&lt;p&gt;A destroy function is used to notify waiting threads to shutdown the program. For clarity this is implemented only in a rudimentary way. For full functionality it must be defined what shall happen when the reader blocks on an empty queue and when the writer blocks on a full queue while the system is to be shut down. There are several solutions for this problem which shall be discussed in a further post.&lt;/p&gt;
&lt;br /&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/stephanpareigis/efd65ceae7925ea243f78fc364037989.js&#34;&gt;&lt;/script&gt;

</description>
    </item>
    
    <item>
      <title>Parameter Handling</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/parameter-handling/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/parameter-handling/</guid>
      <description>&lt;p&gt;Algorithms often need parameters which need to be adjusted while testing the program. Instead of writing all parameters in a .h-file which requires that the code has to be compiled  after each change, we propose a method which allows parameter handling during runtime in a uniform manner.&lt;/p&gt;
&lt;p&gt;A basic requirement for this is, that the parameters may still be used in equations, calculations and conditions without having to deal with bloatware method calls.&lt;/p&gt;
&lt;p&gt;This code sample shows how to wrap atomic types such that they will still feel like an atomic type. However, because your parameters are inside a wrapper, you may now manipulate all your parameters via this wrapper class. For example, you may store all parameters in a list, save them to a file, remote access them with a browser and so on.&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/stephanpareigis/0be7635cc1113a076ff32eb2ae9795e0.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>Singleton</title>
      <link>https://autosys.informatik.haw-hamburg.de/codesamples/singleton/</link>
      <pubDate>Mon, 13 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://autosys.informatik.haw-hamburg.de/codesamples/singleton/</guid>
      <description>&lt;p&gt;A singleton is a class which controls the number of objects to be created from this class. Most often there shall be just a single object from a certain type. The basic principle to reach this is to limit the access to the constructor, i.e. making it private. There is a static function which allowes a single point of access to the single object.&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/stephanpareigis/4e06c6f84e73fb4fada2b4e7f77527da.js&#34;&gt;&lt;/script&gt;

&lt;h1 id=&#34;controversial-discussion&#34;&gt;Controversial discussion&lt;/h1&gt;
&lt;p&gt;There is a controversial discussion if a singleton is good design. The main critique is that it is reachable from any scope, thus appearing as a global variable. It is also not trivial to make it thread-safe (pre C++11). Destruction of a singleton is another issue. Also, lazy initialization might not be what you want in a time-critical system. I personally suggest in reactive time-critical systems to allocate your resources at start-up time if possible. This will guarantee by design that there is only a certain number of objects of a certain type. Also the initialization order may be established easliy. The objects shall communicate via appropriate communication channels.&lt;/p&gt;
&lt;h1 id=&#34;issues&#34;&gt;Issues&lt;/h1&gt;
&lt;p&gt;There are a couple of issues to think about when deciding to use a singleton and choosing the right implementation.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;threadsafety&lt;/strong&gt; of a singleton. There is the DCLP (double checked locking pattern) [1] to make the original GOF singleton [2] thread-safe. As recovered several years later, this may fail under certain circumstances [3]. In C++11 there is a thread-safe implementation due to a new memory model. The Meyers Singleton [5] is also thread-safe in C++11.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;destruction&lt;/strong&gt;. The original GOF singleton cannot be destructed. At the end of the program it doesn´t free its resources. The Meyers Singletons destructor is called at the programs end. If you need to destroy a singleton during runtime and re-create it later, the phoenix singleton [6] comes in handy.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;references&#34;&gt;References&lt;/h1&gt;
&lt;p&gt;[1] D.C. Schmidt, T.Harrison; Double-Checked Locking; Pattern Languages of Program Design 3, 1997&lt;br /&gt;
[2] E.Gamma, R.Johnson, R.Helm, J.Vlissides; Design Pattern; Prentice Hall 1994&lt;br /&gt;
[3] S.Meyers, A.Alexandrescu; C++ and the Perils of Double-Checked Locking; 2004&lt;br /&gt;
[4] H.Sutter; herbsutter.com&lt;br /&gt;
[5] S.Meyers; More Effective C++; 1996&lt;br /&gt;
[6] A.Alexandrescu; Modern C++ Design, Addison-Wesley, 2001&lt;br /&gt;&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
