Q: I have not been able to set the timing to do in C / C + + very well the few times I was obliged to (such as timing performance in clock ticks or milliseconds) to do. I was never a good grasp on what to use timer, since there are many ways to do it, but also as a platform-specific ones such as Linux timer functions.
So what I should do a one second interval. This would be so easy on a microcontroller! It would be: 977 503
while (some condition) 977 503 / / do a lot of things, so its not as simple ISR
end while
I have to walk through a bunch of code within the while loop once per second. I have no idea how long it will take to run code, so its not that I can delay the remaining time of one second each iteration.
Any ideas on how to do what and timer function should I use? It must be very close to 1 seconds (a few clock ticks difference should not matter but 1.1 seconds would not be acceptable).
Re:threads? talk about non-deterministic
Re:A pointer to a shared, persistent structure would work, and could prevent memory leak and heap fragmentation issues.
Just starting a new thread is easier than recycling, but it could work if the worker thread can block when it's complete and restart when the parent unblocks it.
I'm a Win32 developer so I don't know how you'd approach that in linux.
Re:Thanks for the replies all.
guy: Indeed, gettimeofday() was my first idea to accomplish this. I've used it once before but don't completely understand it all. I think gettimeofday() returns a timeval struct and then I need to access its fields to get the current seconds or microseconds. I'll have to look up the documentation.
guy and guy: That's a neat idea and kinda helpful since my thread knowledge is weak. I'll be using pthreads btw. So I believe I'd use the following pthread functions:
Thread thread = new Thread(workerFunction) EQUIVALENT TO: pthread_create(&<thread ID>, NULL, job, (void*) data) // data is a struct for parameters if I need to pass any
thread.terminate() EQUIVALENT TO: pthread.join(<thread ID>, NULL).
One question on that solution, would I necessarily need to create a new worker thread every iteration, or can I create it once and basically restart it?
I'm trying to think if I'd lose any values I'd need to keep between iterations, or if passing it a struct parameter that's a global variable would take care of that. I might need quite a lot of variables for the worker thread to work on and save their values each iteration, such as pointers to several structures and/or classes.
Maybe I should just use vfork() so I get a child process that can use the parent's variables? It would be more costly for the context switch though. And as you said, it would be tough to know if the worker thread has completed its old work. But in my case, it HAS to be complete and do it again precisely every second since it is for a Monte Carlo simulation. I believe that I'll have a lot of code to do in the one second interval (very rough guess of 50-75 lines). They will mostly be fairly basic instructions (shouldn't need MUL/DIV or I/O access), but it's likely I'll have a lot of IFs (branches) so I guess I better make damn well sure I can do everything within the second everytime.
Re:Something like this:
while (1)
{
Thread thread = new Thread(workerFunction);
usleep(1000000);
thread.terminate();
}
Re:Sounds like a job for threads. Start / restart a worker thread once a second, have the main thread go back to sleep immediately.
Or interrupts, set a timer interrupt, (re)start the worker thread whenever it fires.
The only tricky part is knowing not to (re)start the worker thread on a tick if its old work wasn't completed for some reason.
Re:Here's what I'd do:
Edit: removed double code snippet
0 Comments.