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

Web-server & Muti-threading


  • Please log in to reply
5 replies to this topic

#1 Ricco

Ricco

    New Member

  • Members
  • Pip
  • 3 posts

Posted 03 September 2011 - 09:24 AM

Hi, I made some projects with arduino bords. Just polling I/O and printing the result on the serial output. I'm from the pc world, my coding isn't really optimize but it do the works ! Now I would add ethernet output to my project but I fear off communication delay on arduino. I think that if it took to long to speak to a web server, the I/O mesurement will loose some evenements. I read that netduino has multi-thread support. There's maybe an issue with memory place with all the librairies and all but is it possible to poll 10 entries every 1ms, while randomly writing on the sd card and sending packet to a internet web server and envetualy serve web page to be configured ? Is-it reall multi-reading ( as real as it could be on a pc ... ) or just a way to write code ?

#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 03 September 2011 - 04:22 PM

Is-it reall multi-reading ( as real as it could be on a pc ... ) or just a way to write code ?

There is an article on threading here.

I've never tried 10 threads but I have written a few projects with two threads and they work OK. Two projects were web server based projects with the web server in it's own thread and interrupts being processed by the main program loop. I also have a persistence of vision project which has a controller program (in one thread) and a thread which updates the display.

Hope this helps,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 04 September 2011 - 04:25 AM

There is an article on threading here.

I've never tried 10 threads but I have written a few projects with two threads and they work OK. Two projects were web server based projects with the web server in it's own thread and interrupts being processed by the main program loop. I also have a persistence of vision project which has a controller program (in one thread) and a thread which updates the display.

Hope this helps,
Mark


Thanks Mark! I'm glad I read it!

In fact... people that are used to dealing with threads on the PC should note the MAJOR change in philosophy. On a single processor PC… ALL higher priority threads are serviced before ANY lower priority threads. In multiprocessor PC's each thread gets a processor to itself and thus does an equal amount. On the Netduino lower priority get 50% of the time of their next higher priority. I had to try it out to confirm that PC’s “still” work the way I had recalled from years ago. Here is the sample code I ran on both a PC and the Netduino. Note the percentage of calculations for the Lowest priority out of the BelowNormal priority for each device.

PC(1 processor) 1.1%
PC(>1 processor) 100%
Netduino 50%

using System;
using System.Threading;
#if MF_FRAMEWORK_VERSION_V4_1
using Microsoft.SPOT;
#else
using System.Diagnostics;
#endif


namespace ConsoleApplication1
{
    public class Program
    {
        private static int _L, _H;
        private static bool _Run;
        private static int MAX;

        static void Main(string[] args)
        {
            _L = _H = 0;
            _Run = true;

#if MF_FRAMEWORK_VERSION_V4_1
            MAX = 10;
#else
            Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)1;
            MAX = 1000000;
#endif


            Thread h = new Thread(new ThreadStart(HighPriority));
            h.Priority = ThreadPriority.BelowNormal;

            Thread l = new Thread(new ThreadStart(LowerPriority));
            l.Priority = ThreadPriority.Lowest;

            h.Start();
            l.Start();

            Thread.Sleep(100000);
            _Run = false;
            Thread.Sleep(100);

            Debug.Print("Lower = " + _H.ToString());
            Debug.Print("Lowest = " + _L.ToString());

            // Number of times "p" is calculated.
            // Netduino        Lower  546,620     Lowest  273,230
            // PC 1 Processor  Lower  21,256M     Lowest  235M
            // PC # Processors Lower  20,026M     Lowest  20,031M
        }

        private static void LowerPriority()
        {
            int p, i = 0;
            while (_Run)
            {
                i++;
                if (i % MAX == 0)
                {
                    _L++;
                    i = 0;
                }
                p = (int)(i * System.Math.PI);
            }
        }

        private static void HighPriority()
        {
            int p, i = 0;
            while (_Run)
            {
                i++;
                if (i % MAX == 0)
                {
                    _H++;
                    i = 0;
                }
                p = (int)(i * System.Math.PI);
            }
        }
    }
}

Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com

#4 Spork

Spork

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts

Posted 06 September 2011 - 02:56 AM

In multiprocessor PC's each thread gets a processor to itself and thus does an equal amount.


Hi Inq,

I'm not sure that's correct... My understanding is that a multiprocessor PC with N cores will pick the highest N priority tasks to run, at any given time. This may sometimes result in a lower priority thread getting as much CPU time as a higher priority thread, but only if there are no other higher priority threads.

#5 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 06 September 2011 - 11:42 AM

Hi Inq,

I'm not sure that's correct... My understanding is that a multiprocessor PC with N cores will pick the highest N priority tasks to run, at any given time. This may sometimes result in a lower priority thread getting as much CPU time as a higher priority thread, but only if there are no other higher priority threads.


You are correct. I was being way too simplistic versus saying the textbook version as I should have. I was trying to compare and contrast the Netduino behavior with that of the PC’s multithreading and suggest people read the above article. If you are used to the PC’s multitasking behavior and are writing code for the Netduino expecting that behavior, you will be disappointed.

Scenario - You have two threads. Both have no Sleeps or other processor releasing methods. One thread is at a higher priority than the other by one level (say… one thread at BelowNormal and the other at Lowest). The code I supplied above illustrates this scenario…

On a PC 1 – If it is a single processor machine or a heavily loaded multiprocessor machine, it is possible to completely starve the lower priority thread.

On a PC # - If it is a lightly loaded multiprocessor machine (as most developers are developing with) the two threads will in essence get equal time.

On a Netduino – The lower priority thread will get 50% of what the higher priority thread gets. It will not get starved.

I’m sure it won’t be long before these Netduino’s are multi-cored… wonder which behavior we’ll get then. :)
Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com

#6 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 06 September 2011 - 11:57 AM

This is purely subjective and IMHO… I like this Netduino behavior better for what I want to do with it.

I get five levels. I can set up my tasks that I need it to do and prioritize them and simply let it loose. The Netduino will not starve any one of them.

Now this brings up an important question. For those threads that are implicitly created for us...

_highFrequency = new InterruptPort(pinDigital, false, Port.ResistorMode.Disabled, 
    Port.InterruptMode.InterruptEdgeBoth);
_highFrequency.OnInterrupt += new NativeEventHandler(PerHighFrequency);

… what are their thread priority and can we adjust it?
Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com




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.