c++ - Calling functions at timed intervals using threads -


I am building a simulator to test student code for a very simple robot. I need to run two functions (to update robotic sensor and robot status) on different threads at regular time interval. My current implementation is highly processor disabled because it is a thread that only numbers to keep track of status in the code Enhancing is dedicated. My movement theory is that I may be able to use sleep to delay the time between updating the status of the sensor and the robot. My first question is: Is it efficient? Second: is there a way to do a simple job, but to measure the clock cycle instead of seconds?

Thread is usually efficient for sleep by waiting on a mute axis object Waiting on a Mute X involves waiting. When the time limit expires, the gap is over. When the mutation is released, this indicates the end of the thread.

Pseudocode:

  zero thread method () {for (;;) {bool signaled = this -> Mutex.wait (1000); If (hint) {break; // signal, the owner wants to end us) // timeout, which means that it is time to wait for doPeriodicAction (); }} Zero start () {this-> mutex.enter (); This- & gt; Thread.start (threadMethod); } Zero stop () {this-> mutex.leave (); This- & gt; Thread.join (); }  

On Windows systems, timeout is usually specified in milliseconds and may be able to improve approximately 16 milliseconds ( timeBeginPeriod () ) Is accurate for I do not know about a CPU cycle-trigger synchronization primitive, light mutes that spin the CPU for some thousand cycles are called "important squares" before handing over the OS Thread Scheduler. Within this time they are quite accurate

The accuracy of the Linux system may be slightly higher (high frequency timer or tickle kernel) and in addition to mutes, there are "futures" (fast mutes) which are similar to important parts of Windows.


I'm not sure what you are trying to achieve, but if you want to test the student code, you will want to use the virtual clock pass the time yourself To control, for example a processInputs () and decisionmanship () to provide students with the call of the method After each call, 1 time slot is up.


Comments