<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Code examples</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>Recent changes to Code examples</description><atom:link href="https://sourceforge.net/p/posixcpp/wiki/Code%20examples/feed" rel="self"/><language>en</language><lastBuildDate>Tue, 08 Oct 2019 12:47:27 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/posixcpp/wiki/Code%20examples/feed" rel="self" type="application/rss+xml"/><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v7
+++ v8
@@ -216,22 +216,21 @@
 ~~~

 The output may look like
-~~~
-Put 0
-Get 0
-Put 1
-Get 1
-Put 2
-Get 2
-Put 3
-Get 3
-Put 4
-Get 4
-Put 5
-Put 6
-Put 7
-Put 8
-Call condition (errno = pthread_cancel(m_handle)) == 0 at ../src/posix_threads_thread.cpp:134 Unable to cancel thread: No such process
-~~~
+&amp;gt; Put 0
+&amp;gt; Get 0
+&amp;gt; Put 1
+&amp;gt; Get 1
+&amp;gt; Put 2
+&amp;gt; Get 2
+&amp;gt; Put 3
+&amp;gt; Get 3
+&amp;gt; Put 4
+&amp;gt; Get 4
+&amp;gt; Put 5
+&amp;gt; Put 6
+&amp;gt; Put 7
+&amp;gt; Put 8
+&amp;gt; Call condition (errno = pthread_cancel(m_handle)) == 0 at ../src/posix_threads_thread.cpp:134 Unable to cancel thread: No such process
+
 The Reader thread  has no delay in it's loop, but blocks on the queue empty condition every time it gets the last element of the queue.
 The writer thread blocks after inserting 8th element, because reader takes only 5 elements (0-4) and the queue has only 4 elements capacity.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Tue, 08 Oct 2019 12:47:27 -0000</pubDate><guid>https://sourceforge.nete2d03ffb9e2bcba98550d4267a779a82a910c48e</guid></item><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v6
+++ v7
@@ -213,5 +213,25 @@

    return EXIT_SUCCESS;
 }
-
-~~~
+~~~
+
+The output may look like
+~~~
+Put 0
+Get 0
+Put 1
+Get 1
+Put 2
+Get 2
+Put 3
+Get 3
+Put 4
+Get 4
+Put 5
+Put 6
+Put 7
+Put 8
+Call condition (errno = pthread_cancel(m_handle)) == 0 at ../src/posix_threads_thread.cpp:134 Unable to cancel thread: No such process
+~~~
+The Reader thread  has no delay in it's loop, but blocks on the queue empty condition every time it gets the last element of the queue.
+The writer thread blocks after inserting 8th element, because reader takes only 5 elements (0-4) and the queue has only 4 elements capacity.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Tue, 08 Oct 2019 12:45:47 -0000</pubDate><guid>https://sourceforge.netcc6c7d7546858600dbfe5e0e1f478545ed8e5b02</guid></item><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v5
+++ v6
@@ -45,7 +45,7 @@

 ## 2.1 Bounded synchronous queue ##

-~~~
+~~~C++
 #include &amp;lt;cstdlib&amp;gt;

 #include &amp;lt;iostream&amp;gt;
&amp;lt;/iostream&amp;gt;&amp;lt;/cstdlib&amp;gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Tue, 08 Oct 2019 12:41:38 -0000</pubDate><guid>https://sourceforge.net1bcf3051edb28b096012af5b8e54917af0377d86</guid></item><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v4
+++ v5
@@ -1,6 +1,6 @@
 # 1. Asynchronous function call #

-~~~
+~~~C++

 #include &amp;lt;cstdlib&amp;gt;

&amp;lt;/cstdlib&amp;gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Tue, 08 Oct 2019 12:41:13 -0000</pubDate><guid>https://sourceforge.netd9471e72c184d5e15a5e10421c71ea2a8a84f5c2</guid></item><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v3
+++ v4
@@ -44,3 +44,174 @@
 # 2. Synchronization primitives #

 ## 2.1 Bounded synchronous queue ##
+
+~~~
+#include &amp;lt;cstdlib&amp;gt;
+
+#include &amp;lt;iostream&amp;gt;
+#include &amp;lt;queue&amp;gt;
+
+#include "posix_threads_thread.hpp"
+#include "posix_threads_mutex.hpp"
+#include "posix_threads_condition.hpp"
+#include "posix_threads_criticalsection.hpp"
+
+template &amp;lt;typename t=""&amp;gt;
+class BoundedSyncBlockingQueue
+{
+public:
+   
+   explicit BoundedSyncBlockingQueue(size_t capaciy) :
+   m_fullCond(m_lock),
+   m_emptyCond(m_lock),
+   m_capacity(capaciy)
+   {
+   }
+   
+   void enqueue(const T&amp;amp; item)
+   {
+       posix::threads::CriticalSection cs(m_lock);
+       
+       while (m_baseQueue.size() &amp;gt;= m_capacity)
+           m_fullCond.wait();
+       
+       m_baseQueue.push(item);
+       
+       if (m_baseQueue.size() == 1)
+           m_emptyCond.broadcast();
+   }
+   
+   T dequeue()
+   {
+       posix::threads::CriticalSection cs(m_lock);
+       
+       while (m_baseQueue.empty())
+           m_emptyCond.wait();
+       
+       T result = m_baseQueue.front();
+       m_baseQueue.pop();
+       
+       if (m_baseQueue.size() == (m_capacity - 1))
+           m_fullCond.broadcast();
+       
+       return result;
+   }
+   
+   size_t getSize() const
+   {
+       posix::threads::CriticalSection cs(m_lock);
+       
+       return m_baseQueue.size();
+   }
+   
+private :
+   
+   mutable posix::threads::Mutex m_lock;
+   
+   posix::threads::Condition m_fullCond;
+   
+   posix::threads::Condition m_emptyCond;
+   
+   const size_t m_capacity;
+   
+   std::queue&amp;lt;t&amp;gt; m_baseQueue;
+};
+
+static posix::threads::Mutex stdoutLock;
+
+class Reader : public posix::threads::Thread
+{
+public:
+   
+   explicit Reader(BoundedSyncBlockingQueue&amp;lt;int&amp;gt;&amp;amp; q) :
+   m_queue(q)
+   {
+   }
+   
+private:
+   
+   BoundedSyncBlockingQueue&amp;lt;int&amp;gt;&amp;amp; m_queue;
+   
+// posixcpp::ActiveObjectInterface
+public:
+   
+   void run() override
+   {
+       for (uint8_t i=0; i&amp;lt;5; ++i) {
+           int a = m_queue.dequeue();
+           stdoutLock.lock();
+           std::cout &amp;lt;&amp;lt; "Get " &amp;lt;&amp;lt; int(a) &amp;lt;&amp;lt; std::endl;
+           stdoutLock.unlock();
+       }
+   }
+};
+
+class Writer : public posix::threads::Thread
+{
+public:
+   
+   explicit Writer(BoundedSyncBlockingQueue&amp;lt;int&amp;gt;&amp;amp; q) :
+   m_queue(q)
+   {
+   }
+   
+private:
+   
+   BoundedSyncBlockingQueue&amp;lt;int&amp;gt;&amp;amp; m_queue;
+   
+// posixcpp::ActiveObjectInterface
+public:
+   
+   void run() override
+   {
+       for (uint8_t i=0; i&amp;lt;10; ++i) {
+           posix::time::sleep(1);
+           m_queue.enqueue(int(i));
+           stdoutLock.lock();
+           std::cout &amp;lt;&amp;lt; "Put " &amp;lt;&amp;lt; int(i) &amp;lt;&amp;lt; std::endl;
+           stdoutLock.unlock();
+       }
+   }
+};
+
+int
+main(int argc, char** argv)
+{
+   BoundedSyncBlockingQueue&amp;lt;int&amp;gt; queue(4);
+   
+   Reader r(queue);
+   Writer w(queue);
+   
+   r.start();
+   w.start();
+   
+   posix::time::sleep(11);
+   
+   try {
+       r.cancel();
+   } catch (const std::exception&amp;amp; ex) {
+       std::cerr &amp;lt;&amp;lt; ex.what() &amp;lt;&amp;lt; std::endl;
+   }
+   
+   try {
+       w.cancel();
+   } catch (const std::exception&amp;amp; ex) {
+       std::cerr &amp;lt;&amp;lt; ex.what() &amp;lt;&amp;lt; std::endl;
+   }
+   
+   try {
+       r.join();
+   } catch (const std::exception&amp;amp; ex) {
+       std::cerr &amp;lt;&amp;lt; ex.what() &amp;lt;&amp;lt; std::endl;
+   }
+   
+   try {
+       w.join();
+   } catch (const std::exception&amp;amp; ex) {
+       std::cerr &amp;lt;&amp;lt; ex.what() &amp;lt;&amp;lt; std::endl;
+   }
+   
+   return EXIT_SUCCESS;
+}
+
+~~~
&amp;lt;/int&amp;gt;&amp;lt;/int&amp;gt;&amp;lt;/int&amp;gt;&amp;lt;/int&amp;gt;&amp;lt;/int&amp;gt;&amp;lt;/t&amp;gt;&amp;lt;/typename&amp;gt;&amp;lt;/queue&amp;gt;&amp;lt;/iostream&amp;gt;&amp;lt;/cstdlib&amp;gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Tue, 08 Oct 2019 12:37:12 -0000</pubDate><guid>https://sourceforge.netabd1df5f5324c6af3779231b1684aac4b29cd593</guid></item><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v2
+++ v3
@@ -1,10 +1,45 @@
 # 1. Asynchronous function call #

-## 1.1 Thread ##
+~~~

+#include &amp;lt;cstdlib&amp;gt;

+#include &amp;lt;iostream&amp;gt;
+#include &amp;lt;memory&amp;gt;

-## 1.2 Process ##
+#include "posix_threads_thread.hpp"
+#include "posix_process.hpp"
+
+void* asyncFunction(void* param)
+{
+   std::cout &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;
+}
+
+#define PROCESS 0
+#define THREAD 1
+
+//#define MODE PROCESS
+#define MODE THREAD
+
+int
+main(int argc, char** argv)
+{
+   std::unique_ptr&amp;lt;posixcpp::activeobject&amp;gt; activeObject;
+#if MODE == PROCESS
+   activeObject.reset(new posix::Process(asyncFunction));
+#else
+   activeObject.reset(new posix::threads::Thread(asyncFunction));
+#endif
+   
+   activeObject-&amp;gt;start();
+   activeObject-&amp;gt;waitFinish();
+   
+   std::cout &amp;lt;&amp;lt; "Complete" &amp;lt;&amp;lt; std::endl;
+   
+   return EXIT_SUCCESS;
+}
+
+~~~

 # 2. Synchronization primitives #

&amp;lt;/posixcpp::activeobject&amp;gt;&amp;lt;/memory&amp;gt;&amp;lt;/iostream&amp;gt;&amp;lt;/cstdlib&amp;gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Thu, 03 Oct 2019 13:33:36 -0000</pubDate><guid>https://sourceforge.nete38fd0f5ff56496de9a46744a5c99973911463e4</guid></item><item><title>Code examples modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Code%2520examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v1
+++ v2
@@ -1,8 +1,11 @@
-Welcome to your wiki!
+# 1. Asynchronous function call #

-This is the default page, edit it as you see fit. To add a new page simply reference it within brackets, e.g.: [SamplePage].
+## 1.1 Thread ##

-The wiki uses [Markdown](/p/posixcpp/wiki/markdown_syntax/) syntax.

-[[members limit=20]]
-[[download_button]]
+
+## 1.2 Process ##
+
+# 2. Synchronization primitives #
+
+## 2.1 Bounded synchronous queue ##
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Thu, 03 Oct 2019 13:25:49 -0000</pubDate><guid>https://sourceforge.nete90fb5ffa560e0e79960ec3b74bcc7381fadcb26</guid></item><item><title>Home modified by Andrey Skvortsov</title><link>https://sourceforge.net/p/posixcpp/wiki/Home/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;Welcome to your wiki!&lt;/p&gt;
&lt;p&gt;This is the default page, edit it as you see fit. To add a new page simply reference it within brackets, e.g.: &lt;span&gt;[SamplePage]&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The wiki uses &lt;a class="" href="/p/posixcpp/wiki/markdown_syntax/"&gt;Markdown&lt;/a&gt; syntax.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;h6&gt;Project Members:&lt;/h6&gt;
	&lt;ul class="md-users-list"&gt;
		&lt;li&gt;&lt;a href="/u/starling13/"&gt;Andrey Skvortsov&lt;/a&gt; (admin)&lt;/li&gt;
		
	&lt;/ul&gt;&lt;br/&gt;
&lt;p&gt;&lt;span class="download-button-5d94565de396013d5bc3580f" style="margin-bottom: 1em; display: block;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrey Skvortsov</dc:creator><pubDate>Wed, 02 Oct 2019 07:48:45 -0000</pubDate><guid>https://sourceforge.net9771c9bc58de8f93afdfa43bd25ef434de26b4b3</guid></item></channel></rss>