C/C++ on Linux/Unix

Moderators: Lundin
Number of threads: 314
Number of posts: 633

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with monitors in C-- Posted by IamBMF on 27 May 2009 at 7:59 AM
Hello, I started studying BACI recently and I am having some trouble with monitors.
I know this is not strictly C/C++ but I did not know where to post this. I hope someone can help.

I made a very simple application that shows what my problem is:

monitor monSimple
{
    int variable;
   
    void method1()
    {
        cout << "method1: variable = " << variable << endl;
        variable++;
        cout << "method1: variable = " << variable << endl;
    }
   
    void method2()
    {
        cout << "method2: variable = " << variable << endl;
        variable++;
        cout << "method2: variable = " << variable << endl;
    }
    
    init
    {
        variable = 0;
    }
    
} //monitor


main()
{
    cobegin 
    {
        method1();
        method2();
    }
} // main


I expect the output to be:

method1: variable = 0
method1: variable = 1
method2: variable = 1
method2: variable = 2

or

method2: variable = 0
method2: variable = 1
method1: variable = 1
method1: variable = 2.

However, the real output is either:

method1: variable = 0
method1: variable = 2407
method2: variable = 0
method2: variable = 2607

or:

method2: variable = 0
method2: variable = 2607
method1: variable = 0
method1: variable = 2407.

What am I doing wrong?
Report
Re: Problem with monitors in C-- Posted by Lundin on 27 May 2009 at 9:14 AM
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.
Report
Re: Problem with monitors in C-- Posted by IamBMF on 27 May 2009 at 3:40 PM
This is their website.

In case it helps, if the main is changed from:

main()
{
    cobegin 
    {
        method1();
        method2();
    }
} // main


to:

main()
{
   method1();
   method2();
} // main


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.

A monitor is a block so that functions on it will only execute one at a time, basically. Ideal for mutual exclusion problems.
A cobegin a block whose functions will be executed concurrently. For example, you don't know which of the functions will be executed first.

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.
Report
Re: Problem with monitors in C-- Posted by IamBMF on 27 May 2009 at 4:34 PM
It works now.

monitor monSimple
{
    int variable;
   
    void method1()
    {
        cout << "method1: variable = " << variable << endl;
        variable++;
        cout << "method1: variable = " << variable << endl;
    }
   
    void method2()
    {
        cout << "method2: variable = " << variable << endl;
        variable++;
        cout << "method2: variable = " << variable << endl;
    }
    
    init
    {
        variable = 0;
    }
    
} //monitor


void method1aux()
{
    method1();
}

void method2aux()
{
    method2();
}

main()
{
    cobegin 
    {
        method1aux();
        method2aux();
    }
} // main


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.


Report
Re: Problem with monitors in C-- Posted by Lundin on 28 May 2009 at 5:42 AM
That language appears to be quite obscure :)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.