I have an application for which I code with Java and C++. In this application I need to have multiple "trials" that lasts exactly N seconds (let's say N = 15). What I also need is to log the current state every 0.005 seconds. For this exact purpose I have the following code:
std::thread timerTrial(&endTrial,this);
std::thread timerLog(&log,this);
std::thread timerTrial(&endTrial,this);
timerLog.detach();
timerTrial.detach();
void endTrial(){
usleep(TIME);
isOver = true ;
//...
}
void log(){
while(isOver == false){
usleep(TIMELOG);
//Do the logging
}
}
The thing is when I'm looking at my logging (which is then written in a file) I see that I get a different number of logging lines (thus meaning that one trial has logs till 14.5 seconds for instance and the other 14.3 and an other one 14.8). Is there any way to improve this code so as to get less differences between each trial.
I think that the logging that I have to do in log()
may be responsible for a small delay but honestly it is a really small logging (mainly adding things into a std::vector).
Should I create an other parallel thread to do this logging as well so as not to waste time in my while
loop in the log()
function?
I'm running short of ideas to improve this code so as to decrease the difference between each trial. I hope my question is clear enough. If not feel free to comment to ask for further explanations.
Thanks in advance,