Thread locking

All this talk of threads leads me to ask the question about locks to deal with data across threads.

For my rather complex ROV control software, I have a number of threads handling various serial inputs and other tasks that have to run in the background. All good and to handle data I use the following:

object lockThis = new object();
 
bool lockTaken = false;
try
{
    Monitor.Enter(lockThis, ref lockTaken);
    {
        if(lockTaken)
        {
            // Do data handling here 
        }
    }
}
finally
{
    if (lockTaken) 
        Monitor.Exit(temp);
}

Is this available with TinyCLR or just the basic lock{}?

PS… I haven’t tried to use the above code with TinyCLR as I am still working on the hardware.

AFAIK, lock() is the short version of try {Monitor } finally {}.

I use lock() everywhere (where needed) without any glitch so far. But I’m not building airplanes either.

2 Likes

Time for a new project :grin:

3 Likes