Hello everyone,
I’m using a mathematic gradient algorithm for calculations of stuff. This algorithm has to run every couple of milliseconds. At the moment, I’m creating a thread and use the Thread.Sleep() command like this.
private void Start ()
{
// Start the thread that will be the SuperFastLoop
updateThread = new Thread(SuperFastLoop);
updateThread.Priority = System.Threading.ThreadPriority.Highest;
updateThread.Start();
}
private void SuperFastLoop()
{
// This begins our Update loop
while (true)
{
if (SerialComm.Streaming)
{
// Do Algorithm stuff here
}
Thread.Sleep(DelayMs);
}
}
As far as I’ve seen, Thread.Sleep() is called a beginner’s tactic and “one should do it better”, but I have evaluated and tested the common alternatives, and they’re not suitable for a delay of around 1 ms.
Basically, my question is: Is there a proper or better way to have a sharp 1 ms delay between function calls, at best including the algorithm calculation time, on multiple platform?
5 ms would also be suitable and are actually used by now. But I would have it dependent on the system and its power. I has to be way faster than FixedUpdate() and should not be calculated here, as I don’t want to mess with all physics due to that.
We intend to use this on these platforms: Windows, Mac, Android, iOs.