Web-server & Muti-threading
#1
Posted 03 September 2011 - 09:24 AM
#2
Posted 03 September 2011 - 04:22 PM
There is an article on threading here.Is-it reall multi-reading ( as real as it could be on a pc ... ) or just a way to write code ?
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
- Inquisitor likes this
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
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); } } } }
If my message helped you... how 'bout giving me a
www.MessingWithReality.com
#4
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
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.
If my message helped you... how 'bout giving me a
www.MessingWithReality.com
#6
Posted 06 September 2011 - 11:57 AM
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?
If my message helped you... how 'bout giving me a
www.MessingWithReality.com
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users