Advertisement Jump to content
  • Advertisement

thefallentree

Member
  • Content Count

    3
  • Joined

  • Last visited

Community Reputation

1 Neutral

About thefallentree

  • Rank
    Newbie

Personal Information

  • Interests
    Programming
  1. Thanks for the reply, as far as I can remember, I seems to recall python used an similar approach, and in CPython there is an magic number of instructions executed before an timeout check is performed, sorry that I currently couldn't find the link anywhere. The more I dive into the timekeeping world the more issues that may arise. for example, 1. https://blog.packagecloud.io/eng/2017/03/08/system-calls-are-much-slower-on-ec2/ 2. https://stackoverflow.com/questions/44020619/queryperformancecounter-on-multi-core-processor-under-windows-10-behaves-erratic So my next attempt solution is exactly what you are describing. Use whatever most precise time keeping method on each platform and then use an magic constant to executing a bunch of instructions in an batch before checking timeout. This will compensate the cost of the time reading functions. (maybe it should be called batching factor) Hmm, maybe it is also possible to run an micro-benchmarks on startup , or even during compile time, to get an more precise number Cheers.
  2. The question boils down to this: I have a loop for(;;;) { do_something() } which potentially can run forever, and I want to be able to set an time limit for this function , so currently we have this bool stop = false; for(;;;) { if(stop) break; do_something()} and this stop flag is set through an signal callback from timer_create() under linux. Now I can do this int start_time=now() for(;;;) { if (now() > start_time + 10ms) break; do_something() } but this is a very tight loop , each do_something() is only about several nano seconds. now() however using gettimeofday() or similar is very costly , not to mention i also need to support Windows. If I were to have another thread that does sleep(10); stop=true; I fear the OS thread scheduler make no guarantee how long this thread is starved and it will run past the deadline. So, I'm back to drawing board on how to let kernel call me back at preciser time, which is what timer_create() is for and there is no equivalent under windows (right?). Let me know if this is better,Thanks.
  3. Hi there! I'm currently maintaining an game engine that implements an interpreter VM (https://github.com/fluffos/fluffos if anyone's interested). this program was written in mostly C style C++ code, and it is single thread with an main-loop that runs events , loads/compile files and execute them in an interpreter. which is really just an for(pc;eval(pc);pc++) kind of thing. Now, there is a feature to limit the time spent running this potentially unstopping loop, to accompany this, there is an volitle flag variable created globally and during the loop this flag is checked everytime to see if it should break out . and this flag was set through signal handler. 1. sigalarm , which has limited timing resolution and not cross-platform 2. posix timer : timer_create() etc , which uses monotoic clock provided in linux env, which works better than sigalarm. Now I have need to port this to native windows, which obviously don't have these , so I have a few ideas: 1. write a wrapper for gettimeofday()/clock_gettime()/chrono::time functions, and directly check this during the loop, but the problem is that this will waste a lot of CPU reading and comparing time, which is unacceptable in this performance critical loop. 2. adding an new thread that basically sleep and wakeup to set the variable: I havn't tried this yet, but wouldn't this only rely on thread scheduler performance? the resolution i want is probably at least 1ms , maybe ~10ms is okay. 3. i read under windows one should use QueryPeforamnceCounter() to get precise time, however this is the same problem with (1), i probably shouldn't keep reading the counter in that tight loop. So, here is my question, I really just want an cross-platform function that basically says: run this handler after 10ms, ideally also preempting all my other threads. Is it possible? what's the best way to do that? Thanks
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!