Question

I've been looking through the internet a long time now for an easy solution to a rather easy problem.

I have this function running as a thread, and what I want is quite simple. There is plenty of pseudo code around that explains how it's done, but I'm having trouble implementing this in C/C++.

What I want: I have a loop, which I want to run 20 times a second. I have done this before, but I cannot remember for the life of me.

Here is the code I have.

void* Tick::startBeating() {
    mNext = clock();
    mRunning = 1;

    // Loop dat
    while (mRunning) {
        mT = clock();
        while ((mT) >= mNext) {
            // do updates here
            std::cout << "Tick." << std::endl;

            mNext = mT + SKIP_TICKS;
        }


        sleep(1);
    }

    return NULL;
}

This doesn't work, nothing seems to work. A simple solution in C is no where to be found on the internet, and that surprises me.

Yes, I know about CLOCKS_PER_SEC but I just don't know anymore.

Any help?

Was it helpful?

Solution

I do it like this

float mAccumulatedTime;
int mLastTime;

void Init()
{
  mAccumulatedTime = 0.f;
  mLastTime=clock();
}

void Tick(float DeltaTime)
{
  mAccumulatedTime += DeltaTime;
  while (mAccumulatedTime > FixedTimeStep)
  {
    DoFixedTimeStepStuff();
    mAccumulatedTime -= FixedTimeStep;
  }
}

void Update()
{
  int CurrentTime=clock();
  float DeltaTime=(CurrentTime-mLastTime)/(float)CLOCKS_PER_SEC;
  Tick(DeltaTime);
  mLastTime=CurrentTime;
}

You need to take care to handle NaNs and "spirals of death" appropriately, but this will be enough to get you started.

OTHER TIPS

  1. Before you do work, check the time.

  2. Compute the time you should run the next loop.

  3. Do whatever you need to do.

  4. Compute how much time is left until the time you computed in step 2.

  5. Sleep for that long.

  6. Go to step 1.

The answer depends on needed accuracy. You mentioned that you are running your function as a separate thread

  1. Your thread must do nothing except check time and inform other subsystem about a tick (this means no sleep) while(1) if (elapsed_time >= target_time) flag=1
  2. Another function (maybe main()) has to check the flag and respond while(1) if (thread.flag) {std::cout << "beat\n"; thread.flag=0;}
  3. If your application is rather complex, then you should implement signal/slot mechanism

I am not sure if you don't know how to get the time in C++, or you don't know the steps.

To get the time in MS, if you are on window. you can use GetTickCount() . Information below. http://msdn.microsoft.com/en-us/library/ms724408%28v=vs.85%29.aspx

.

.

If you already can get the time but need the algorithm to do it. For algorithm as to how to do it 20 frames per second, I have an accurate but slightly harder to understand way of doing it.

Here's a brief pseudocode.

CurrentTickCount = GetTickCount();

//The CurrentTime minusing the previous time at the end of this loop gives us the time taken thus far

timeTakenSoFar += CurrentTickCount - PreviousTickCount;

//TickCount is integer, of millisecond value, therefore 20 of these millisecond is equivalent of 20/1000 of a second, In case if I remember or do it wrongly here it would be an easy fix on your side :) while(timeTakenSoFar < 20)

{

PreviousTickCount = CurrentTickCount;

CurrentTickCount = GetTickCount();

timeTakenSoFar += CurrentTickCount - PreviousTickCount;

}

timeTakenSoFar -= 20;

//basically the PreviousTickCount keep track of what's the last Time;

PreviousTickCount = CurrentTickCount;

//comment // I Did not reset timeTakenSoFar to 0, because there will definitely be a slight excess time, the system can not give you exactly 20/1000, therefore this will be more accurate than the wait solution. After running for 10000000000 cycles this will still give you the exact correct time, but not with the wait method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top