<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Problem with monitors in C--' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Problem with monitors in C--' posted on the 'C/C++ on Linux/Unix' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sun, 19 May 2013 04:18:08 -0700</pubDate>
    <lastBuildDate>Sun, 19 May 2013 04:18:08 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Problem with monitors in C--</title>
      <link>http://www.programmersheaven.com/mb/ConLunix/391593/391593/problem-with-monitors-in-c--/</link>
      <description>Hello, I started studying BACI recently and I am having some trouble with monitors.&lt;br /&gt;
I know this is not strictly C/C++ but I did not know where to post this. I hope someone can help.&lt;br /&gt;
&lt;br /&gt;
I made a very simple application that shows what my problem is:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
monitor monSimple
{
    int variable;
   
    void method1()
    {
        cout &amp;lt;&amp;lt; "method1: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
        variable++;
        cout &amp;lt;&amp;lt; "method1: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
    }
   
    void method2()
    {
        cout &amp;lt;&amp;lt; "method2: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
        variable++;
        cout &amp;lt;&amp;lt; "method2: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
    }
    
    init
    {
        variable = 0;
    }
    
} //monitor


main()
{
    cobegin 
    {
        method1();
        method2();
    }
} // main
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
I expect the output to be:&lt;br /&gt;
&lt;br /&gt;
method1: variable = 0&lt;br /&gt;
method1: variable = 1&lt;br /&gt;
method2: variable = 1&lt;br /&gt;
method2: variable = 2&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
method2: variable = 0&lt;br /&gt;
method2: variable = 1&lt;br /&gt;
method1: variable = 1&lt;br /&gt;
method1: variable = 2.&lt;br /&gt;
&lt;br /&gt;
However, the real output is either:&lt;br /&gt;
&lt;br /&gt;
method1: variable = 0&lt;br /&gt;
method1: variable = 2407&lt;br /&gt;
method2: variable = 0&lt;br /&gt;
method2: variable = 2607&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
method2: variable = 0&lt;br /&gt;
method2: variable = 2607&lt;br /&gt;
method1: variable = 0&lt;br /&gt;
method1: variable = 2407.&lt;br /&gt;
&lt;br /&gt;
What am I doing wrong?&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/ConLunix/391593/391593/problem-with-monitors-in-c--/</guid>
      <pubDate>Wed, 27 May 2009 07:59:23 -0700</pubDate>
      <category>C/C++ on Linux/Unix</category>
    </item>
    <item>
      <title>Re: Problem with monitors in C--</title>
      <link>http://www.programmersheaven.com/mb/ConLunix/391593/391598/re-problem-with-monitors-in-c--/#391598</link>
      <description>This is the first time I hear of this "BACI". While I have no idea of the syntax, my guess is that the variable isn't initialized to zero properly.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/ConLunix/391593/391598/re-problem-with-monitors-in-c--/#391598</guid>
      <pubDate>Wed, 27 May 2009 09:14:43 -0700</pubDate>
      <category>C/C++ on Linux/Unix</category>
    </item>
    <item>
      <title>Re: Problem with monitors in C--</title>
      <link>http://www.programmersheaven.com/mb/ConLunix/391593/391606/re-problem-with-monitors-in-c--/#391606</link>
      <description>&lt;a href="http://inside.mines.edu/fs_home/tcamp/baci/"&gt;This is their website.&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
In case it helps, if the main is changed from:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
main()
{
    cobegin 
    {
        method1();
        method2();
    }
} // main
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
to:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
main()
{
   method1();
   method2();
} // main
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Then it outputs the 'expected' result (and the value in the init block initializes the variable properly in this case). I think this proves the monitor's code right. There could be some interference between cobegin and monitor.&lt;br /&gt;
&lt;br /&gt;
A monitor is a block so that functions on it will only execute one at a time, basically. Ideal for mutual exclusion problems. &lt;br /&gt;
A cobegin a block whose functions will be executed concurrently. For example, you don't know which of the functions will be executed first.&lt;br /&gt;
&lt;br /&gt;
Calling monitor functions inside a cobegin block, I am just trying to make a program where any of the monitor functions may be called first, but only one is running at a time.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/ConLunix/391593/391606/re-problem-with-monitors-in-c--/#391606</guid>
      <pubDate>Wed, 27 May 2009 15:40:14 -0700</pubDate>
      <category>C/C++ on Linux/Unix</category>
    </item>
    <item>
      <title>Re: Problem with monitors in C--</title>
      <link>http://www.programmersheaven.com/mb/ConLunix/391593/391607/re-problem-with-monitors-in-c--/#391607</link>
      <description>It works now.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
monitor monSimple
{
    int variable;
   
    void method1()
    {
        cout &amp;lt;&amp;lt; "method1: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
        variable++;
        cout &amp;lt;&amp;lt; "method1: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
    }
   
    void method2()
    {
        cout &amp;lt;&amp;lt; "method2: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
        variable++;
        cout &amp;lt;&amp;lt; "method2: variable = " &amp;lt;&amp;lt; variable &amp;lt;&amp;lt; endl;
    }
    
    init
    {
        variable = 0;
    }
    
} //monitor


void method1aux()
{
    method1();
}

void method2aux()
{
    method2();
}

main()
{
    cobegin 
    {
        method1aux();
        method2aux();
    }
} // main&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Apparently you can't call monitor functions directly from inside a cobegin block (which is something I found by trial and error, it's not in the documentation as far as I can see). So I just enclosed the calls in auxiliary functions and it works.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/ConLunix/391593/391607/re-problem-with-monitors-in-c--/#391607</guid>
      <pubDate>Wed, 27 May 2009 16:34:06 -0700</pubDate>
      <category>C/C++ on Linux/Unix</category>
    </item>
    <item>
      <title>Re: Problem with monitors in C--</title>
      <link>http://www.programmersheaven.com/mb/ConLunix/391593/391648/re-problem-with-monitors-in-c--/#391648</link>
      <description>That language appears to be quite obscure :)</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/ConLunix/391593/391648/re-problem-with-monitors-in-c--/#391648</guid>
      <pubDate>Thu, 28 May 2009 05:42:36 -0700</pubDate>
      <category>C/C++ on Linux/Unix</category>
    </item>
  </channel>
</rss>