Multithreading with Netduino and .NET Microframework – part 2


(to part 1, part 3 or part 4)

In previous part we have seen how to use Suspend() and Resume() methods of Thread class for doing multithreading. There is a problem with sharing resources when using those methods. Fortunately, there are several ways to solve this problems. We will have look on them separately as they are solving different problems.

As a beginning, we’ll look at  method attribute MethodImpl and more specifically MethodImplOptions enumeration. Let’s say we have a method, called by multiple threads, that is doing some unique work and it have to finish it’s execution before next thread can call it. For this case we can attribute the method like this:

[MethodImpl(MethodImplOptions.Synchronized)]
private void SomeSynchronizedMethod()
{
     // Method's code
     ...
}

All threads trying to access this method will be blocked for the time the method is in use from other thread.

Event that there are enough scenarios that justify existence of this approach, you can see many ones that this is absolutely inappropriate. Better approach is to lock the resources that methods are sharing. For this there is a class called Monitor. Using this class is easy:

private static SomeThreadMethod()
{
     ...
     try
     {
          Monitor.Enter(someSharedResource);

          //access shared resource here
          ...
     }
     finally
     {
          Monitor.Exit(someSharedResource);
          ...
     }
     ...
}

private static SomeOtherThreadMethod()
{
     ...
     try
     {
          Monitor.Enter(someSharedResource);

          //access shared resource here
          ...
     }
     finally
     {
          Monitor.Exit(someSharedResource);
          ...
     }
     ...
}

Now we can execute both methods in parallel. When one of them reach Monitor.Enter() it gets access to the shared resource and then performs operation with it. At this point, if other method call Monitor.Enter() to access same resource, it will be blocked until first method execute Monitor.Exit() to release the resource. Note that Monitor.Exit() is in finally block of exception handler. This is best practice; otherwise, if exception occurs within the method, the resource may stay locked forever.

This, you may know, is equivalent to C#’s lock keyword. When compiler meets:

lock(someSharedResource)
{
     // access shared resource here
     ...
}

it compiles it to:

try
{
     Monitor.Enter(someSharedResource);

     //access shared resource here
     ...
}
finally
{
     Monitor.Exit(someSharedResource);
}

The Monitor and Thread classes are part of  System.Threading namespace. As I mentioned before, there are few other classes and each one of them provides us with different way to solve our problems. As conclusion of this part, we will have a quick look on how to use Interlocked class and its members. It is simple as this:

public class Program
{
     private static int someSharedResource;
     ...

     private static void SomeThreadMethod()
     {
          //...
          Interlocked.Increment(ref someSharedResource);
          //...
     }

     private static void SomeOtherThreadMethod()
     {
          //...
          Interlocked.Increment(ref someSharedResource);
          //...
     }
}

or

public class SharedResources
{
     public static int SomeSharedResource;
}

public class Program
{
     private static void SomeThreadMethod()
     {
          //...
          Interlocked.Increment(ref SharedResources.SomeSharedResource);
          //...
     }

     private static void SomeOtherThreadMethod()
     {
          //...
          Interlocked.Decrement(ref SharedResources.SomeSharedResource);
          //...
     }
}

Exchange() and CompareExchange() provides some interesting possibilities, so spend few moments to read the documentation.

We are not done yet with the subject of multithreading, so be ready for more to come in next part.

(to part 1, part 3 or part 4)

Advertisements

6 comments on “Multithreading with Netduino and .NET Microframework – part 2

  1. Pingback: Georgi's blog

  2. Pingback: Multithreading with Netduino and .NET Microframework – part 1 « Georgi's blog

  3. Pingback: Multithreading with Netduino and .NET Microframework – part 3 « Georgi's blog

  4. Pingback: Multithreading with Netduino and .NET Microframework – part 4 « Georgi's blog

  5. I have a problem with Netduino plus 2 and I don’t know how resolve it. I work with OS Win7 and I have downloaded the programs for to work with Netduino plus 2 from official website. I am doing test of exception error on the code and when I run the code line of the socket the program stops and exits the compiler. Obviously I want to treat socket error when network cable is disconnected but I get the error mentioned above. Can you help me? Thanks

    • Hi,
      Unfortunately I haven’t played with Netduino Plus 2 yet. I would suggest you to visit Netduino forums (http://forums.netduino.com/) where you can raise your question. I’m sure you’ll get this sorted out quickly and keep enjoying your NP2.
      Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s