Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Multithreading


Best Answer brianmakabro, 15 April 2015 - 02:50 PM

You can re-use threads... but you can't re-use ThreadStart... very simple example below.   I personally don't think doing stuff in a "while loop" is bad - it all depends on what you are trying to do, and without seeing an example of your code it's all just a guess... anyway, I beg forgiveness for this cheesy example but maybe it will illustrate what I mean:

 

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
 
namespace TestMultiThread
{
    public class Program
    {
        static Thread threadOne;
        static Thread threadTwo;
        static Thread threadThree;
 
        static bool threadOneWorking = false;
        static bool threadTwoWorking = false;
        static bool threadThreeWorking = false;
        
        public static void Main()
        {
            while (1 == 1)
            {
                if (!threadOneWorking)
                {
                    threadOneWorking = true;
                    threadOne = new Thread(new ThreadStart(DoThreadOneWork));
                    threadOne.Start();
                }
 
                if (!threadTwoWorking)
                {
                    threadTwoWorking = true;
                    threadTwo = new Thread(new ThreadStart(DoThreadTwoWork));
                    threadTwo.Start();
                }
 
                if (!threadThreeWorking)
                {
                    threadThreeWorking = true;
                    threadThree = new Thread(new ThreadStart(DoThreadThreeWork));
                    threadThree.Start();
                }
 
                Thread.Sleep(100);
            }
        }
 
        private static void DoThreadOneWork()
        {
            for (int i = 0; i < 10000; i++) ;  // some arbitrary threadOne "work"...
 
            // set this thread's working flag to false on exit...
            threadOneWorking = false;
        }
 
        private static void DoThreadTwoWork()
        {
            for (int i = 0; i < 20000; i++) ;  // some arbitrary threadTwo "work"...
            
            // set this thread's working flag to false on exit...
            threadTwoWorking = false;
        }
 
        private static void DoThreadThreeWork()
        {
            for (int i = 0; i < 30000; i++) ; // some arbitrary threadThree "work"...
            
            // set this thread's working flag to false on exit...
            threadThreeWorking = false;
        }
    }
}
Go to the full post


  • Please log in to reply
6 replies to this topic

#1 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 15 April 2015 - 11:10 AM

Hi,

 

my problem is, that i have got more Threads, in the first thread i ask wich mode u want to execute, for example mode 1, 2 or 3, if u need 3 then my thread: "mode 3" will be activated and executed, and when its work finished, i want to go to my first thread were i ask for the mode u want to execute. If u say again u want mode 3 and u want to execute it, its not possible, cause NET dont allow the reuse of threads. So i need to put all my threads in while loops, wich is no good programming style, and loaded the processor, anyway its almost impossible with loops to realize it. Is there any way to restart a thread again after its execution, or do i have to use a "Threadpool" by wich I am not familiar with, cause my c# experience level is beginner. Can I use tasks on the netduino? If i would use Interrupts, are there any ways to block interrupts for a several time, or when another interrupt is executet?

 

Thanks in advance

 

 

kalio



#2 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 15 April 2015 - 02:50 PM   Best Answer

You can re-use threads... but you can't re-use ThreadStart... very simple example below.   I personally don't think doing stuff in a "while loop" is bad - it all depends on what you are trying to do, and without seeing an example of your code it's all just a guess... anyway, I beg forgiveness for this cheesy example but maybe it will illustrate what I mean:

 

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
 
namespace TestMultiThread
{
    public class Program
    {
        static Thread threadOne;
        static Thread threadTwo;
        static Thread threadThree;
 
        static bool threadOneWorking = false;
        static bool threadTwoWorking = false;
        static bool threadThreeWorking = false;
        
        public static void Main()
        {
            while (1 == 1)
            {
                if (!threadOneWorking)
                {
                    threadOneWorking = true;
                    threadOne = new Thread(new ThreadStart(DoThreadOneWork));
                    threadOne.Start();
                }
 
                if (!threadTwoWorking)
                {
                    threadTwoWorking = true;
                    threadTwo = new Thread(new ThreadStart(DoThreadTwoWork));
                    threadTwo.Start();
                }
 
                if (!threadThreeWorking)
                {
                    threadThreeWorking = true;
                    threadThree = new Thread(new ThreadStart(DoThreadThreeWork));
                    threadThree.Start();
                }
 
                Thread.Sleep(100);
            }
        }
 
        private static void DoThreadOneWork()
        {
            for (int i = 0; i < 10000; i++) ;  // some arbitrary threadOne "work"...
 
            // set this thread's working flag to false on exit...
            threadOneWorking = false;
        }
 
        private static void DoThreadTwoWork()
        {
            for (int i = 0; i < 20000; i++) ;  // some arbitrary threadTwo "work"...
            
            // set this thread's working flag to false on exit...
            threadTwoWorking = false;
        }
 
        private static void DoThreadThreeWork()
        {
            for (int i = 0; i < 30000; i++) ; // some arbitrary threadThree "work"...
            
            // set this thread's working flag to false on exit...
            threadThreeWorking = false;
        }
    }
}


#3 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 15 April 2015 - 02:59 PM

I'll edit my above statement that "you can re-use Thread, but you can't re-use ThreadStart" - it's a misstatement.  The actual Thread(s) are always "new Thread" - AND "new ThreadStart" - but I think the example shows what I mean. Sorry if I'm confusing the situation.



#4 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 16 April 2015 - 11:20 AM

Awesome!

 

That helps me alot, but one i have one question left, if i "re-use" Threads or "new Threads" isnt this a high load for the processor?

 

Thanks in advance!

 

Greets kalio



#5 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 17 April 2015 - 09:46 PM

No, not really.  Of course, it takes memory to start threads, but the garbage collector will (hopefully) reclaim that as the threads are done and no longer "in scope".  I'm sure there is a limit to the number of simultaneous active threads the Netduino can handle - but I don't know what that is.  It's also dependent on what each thread is doing, how many variables, etc. are being instantiated, and such.  I don't think 3 or 4 threads are going to break the bank unless you're really doing something processor-intensive in them.  Maybe Chris could answer that?

 

Welcome

Brian



#6 EddieGarmon

EddieGarmon

    Member

  • Members
  • PipPip
  • 24 posts

Posted 22 April 2015 - 10:41 AM

Ideally you limit creation of items you need and reuse where possible, especially when long lived (such as threads usually are). How long does your mode take to execute?

 

I have recently published a Task implementation to NuGet that supports NETMF 4.2 and 4.3 at:

http://www.nuget.org...ober.Threading/

 

Sounds like you are trying to implement a state machine? If so, other approaches would have better performance.



#7 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 25 April 2015 - 05:09 PM

Hi Eddie Garmon,

 

yes its a state Machine, and I almost solved my Problem with Interrupts :D but i will soon have a look at ur task implementation to nugget.

Thabks for your reply!

 

Greets

 

kalio






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.