I'm currently trying to implement a real-time multithreading software in C#. I need 3 threads. Every thread execution has to be finished before a deadline (500µs / 100µs / 50µs). The threads must run parallel during the whole runtime (until the user shuts down the program).
Is there a mecanism that can guarantee that the thread execution will not pass the deadline?
Here is my code :
static void Main(string[] args)
{
Thread thread1 = new Thread(FirstThread);
Thread thread2 = new Thread(SecondThread);
Thread thread3 = new Thread(ThirdThread);
thread1.start();
thread2.start();
thread3.start();
}
static void FirstThread()
{
while(true)
{
SleepMicroSec(500);
}
}
static void SecondThread()
{
while(true)
{
SleepMicroSec(100);
}
}
static void ThirdThread()
{
while(true)
{
SleepMicroSec(50);
}
}
private static void SleepMicroSec(long microSec)
{
var sw = Stopwatch.StartNew();
while (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L)) < microSec)
{
}
}
I expect the scheduler to be able to perform the context switching if the Task deadline is reached.
Thanks in advance for your answers !