Mutex in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Mutex in NXC

Post by h-g-t »

Been searching the 'net, reading the guides which came with the Bricx Center, etc and I still don't understand mutex.

Does it stop all activity in other tasks while in operation or just prevent the use of any resources used between the two mutex statements?

Advice would be appreciated (preferably in words of half a syllable) or directions to sources of info.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Mutex in NXC

Post by mattallen37 »

Have you heard of the "talking spoon"? It is a general term referring to the method of keeping order (with a spoon), that gets passed around to different individuals. Each person can only talk when they have the spoon. There may be 10 people, but there is only one spoon. In this way, the "talk" will be limited to one person at a time. A mutex was described to me using the same concept. A mutex is a "spoon", and there are (for instance) 10 tasks running. Only the one that "holds" the "spoon" will be able to do anything. To get the "spoon" (mutex), you use the Acquire("mutex"); function ("mutex" is the name of the mutex you want to aquire). To pass the "spoon" to another task, use the Release("mutex"); function.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: Mutex in NXC

Post by h-g-t »

Right, so it stops everything else working - thanks for such a fast response.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Mutex in NXC

Post by muntoo »

To be exact, it only stops the task when it Acquires for something.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Mutex in NXC

Post by m-goldberg »

To be a little more exact, when one task acquires a mutex, any other tasks that try to acquire the same mutex are blocked until the first task releases the mutex. At that time, one of the blocked tasks will acquire the mutex and proceed. The other blocked tasks remain blocked.
Regards, Morton
ronmcrae
Posts: 33
Joined: 28 Sep 2010, 14:56

Re: Mutex in NXC

Post by ronmcrae »

m-goldberg wrote:To be a little more exact, when one task acquires a mutex, any other tasks that try to acquire the same mutex are blocked until the first task releases the mutex. At that time, one of the blocked tasks will acquire the mutex and proceed. The other blocked tasks remain blocked.
Exactly right. However I wanted to point out that Mutexes are largely used behind-the-scenes by the NXC/NBC compiler so that you program does what you intend it to do when multiple tasks access the same resource. In all the NXC programming I have done I have never had to use a Mutex at the user level. Sure, there are likely legitimate reasons it would be needed, but generally it's not something you need to know about.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Mutex in NXC

Post by muntoo »

m-goldberg wrote:To be a little more exact...
ronmcrae wrote: Exactly right.
What, level 1.5 exactness not good enough for you? ;)

Basically, this is how mutexes could be implemented (to give the OP a little more understanding):

Code: Select all

#define mutex bool // so mutex is equivalent to saying bool
//typedef bool mutex; // if you're insane, you could also say this instead of the #define


#define FREE 0
#define USED 1


// Not using safecall, because that would imply recursion... I'll leave the geniuses to figure out that cryptic statement
/*safecall*/ inline void Acquire(mutex &var)
{
// Wait till var == FREE
    while(var == USED);
// Claim it for the task that calls this
    var = USED;
}


/*safecall*/ inline void Release(mutex &var)
{
    var = FREE;
}
The above is just an example, of course. Don't actually use this, because it might fail.

Here is how you would use it:

Code: Select all

mutex mMotorA;


task GoForward()
{
    while(1)
    {
        Acquire(mMotorA);
        OnFwd(OUT_A, 100);
        Wait(1000);
        Release(mMotorA);
    }
}

task GoBackward()
{
    while(1)
    {
        Acquire(mMotorA);
        OnRev(OUT_A, 100);
        Wait(1000);
        Release(mMotorA);
    }
}

task main()
{
    // Start tasks
    Precedes(GoForward, GoBackward);
}
Last edited by muntoo on 11 Jan 2011, 03:15, edited 1 time in total.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Mutex in NXC

Post by mattallen37 »

muntoo wrote:Here is how you would use it:

Code: Select all

mutex mMotorA;


task GoForward()
{
    while(1)
    {
        Acquire(mMotorA);
        OnFwd(OUT_A, 100);
        Release(mMotorA);
    }
}

task GoBackward()
{
    while(1)
    {
        Acquire(mMotorA);
        OnRev(OUT_A, 100);
        Release(mMotorA);
    }
}

task main()
{
    // Start tasks
    Precedes(GoForward, GoBackward);
}
Although, in that example, there aren't any wait functions, so it would change direction way too fast to go much of anywhere.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Mutex in NXC

Post by muntoo »

mattallen37 wrote:
muntoo wrote:Here is how you would use it:

Code: Select all

mutex mMotorA;


task GoForward()
{
    while(1)
    {
        Acquire(mMotorA);
        OnFwd(OUT_A, 100);
        Release(mMotorA);
    }
}

task GoBackward()
{
    while(1)
    {
        Acquire(mMotorA);
        OnRev(OUT_A, 100);
        Release(mMotorA);
    }
}

task main()
{
    // Start tasks
    Precedes(GoForward, GoBackward);
}
Although, in that example, there aren't any wait functions, so it would change direction way too fast to go much of anywhere.
Did you try running it? ;) Although, I fixed my original code.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Mutex in NXC

Post by m-goldberg »

ronmcrae wrote:Exactly right. However I wanted to point out that Mutexes are largely used behind-the-scenes by the NXC/NBC compiler so that you program does what you intend it to do when multiple tasks access the same resource. In all the NXC programming I have done I have never had to use a Mutex at the user level. Sure, there are likely legitimate reasons it would be needed, but generally it's not something you need to know about.
What you say here interests me. How would handle this situation?

Suppose you were thinking implementing a program that appeared to require the classic producer/consumer design pattern. That is, where two tasks share a global array as a data buffer which one tasked fills and the other task empties. AFAIK, the NXC will not recognize this situation as requiring a mutex, so the programmer would have to declare one and use Acquire and Release to implement the required critical sections.

Are you saying 1) you can always avoid using this design pattern or 2) you can always eliminate the explicit mutex by implementing the buffer sharing in a shared function protected by safecall?
Regards, Morton
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest