Hello, I need a program that calls out some code after every x milliseconds and in the same time it waits for keyboard strokes to change the interval time and code what is called out. I should use threads (one for waiting for keyboard input and others for intervalled wake-ups). I need real multi-threading because more than 3 sequences will be called out in the same time.
Using the threads there may be need for complicated thread synchronization? Important is that the App is CPU thrifty (sparing). There are some timer functions which freezes the whole cpu. So how to make safe multi-threading and which function should I use to get those intervals /sleep()?/ and also to get the keystrokes?
My real time application should take control over MIDI device.
P4, Win XP, Visual C++.
Thanks in advance.
Don't expect to get very accurate timeings in the threads because XP (or any other version of MS-Windows or *.nix) is not a real-time operating system. So if you expect an event to happen in exactly X milliseconds it probably won't happen. Probably the best you can hope for is X +/- 10 ms. If you need more accuracy then get a different operating system. To improve this you can strip the computer of all non-essential programs and dedicate it soley to your program.
win32 api function CreateThread() can be used to create the threads. At first glance it looks pretty difficult in MSDN, but many of the parameters can be left 0.
Sleep(milliseconds), WaitForSingleObject() and WaitForMultipleObjects() all put the thread to sleep until a specific event happens. The thread gets almost no CPU time while asleep.
+/- 10 ms accuracy is acceptable.
I´m not so sure any more if I need SAFE multi-threading when I have to send very short messages (max. 12 digits) to the midi port. I mean two threads can not send messages to the midi port exactly the same time because there is no parallel processing in CPU. Maybe mutex´s WaitForSingleObject and ReleaseMutex are not necessary.
What if the two sleep() times ends exactly the same time? Which will be chosen firstly? The last question is not important for me (because it doesn´t make any sense if one message goes a bit earlier) but if it is critical (i mean producing errors)?