View Single Post
Posts: 1,808 | Thanked: 4,272 times | Joined on Feb 2011 @ Germany
#4
Originally Posted by pichlo View Post
It is a GUI app that, depending on settings, may want to fill up a large (200MB+) buffer with data generated at runtime. In pseudocode...
Code:
for (int i; i < 200MB; ++i)
  buffer[i] = fx(i);
fx() can be quite a complex function and running it 200M times can take up to a few minutes.
$ man sched_yield

add #include <sched.h>

and do something along the lines of:

Code:
for (int i; i < 200MB; ++i) {
  if(i % 1000 == 0) sched_yield();
  buffer[i] = fx(i);
}
If that's too ugly for you you could measure the elapsed time (at every iteration, or after N interations) and only when the time is greater than some threshold, you call sched_yield().
 

The Following 6 Users Say Thank You to reinob For This Useful Post: