开发者

writing a string every n milliseconds

I am trying to write a simple win32 console application in C to simulate stock price ticks, I need to specify the time interval so that every n milliseconds a new price is published.

The end goal is to write test data to a database and stress test an application which is supposed to react to new ticks and 开发者_Go百科perform calcs.

My simple price server will be structured as follows

int main (void)
{
    int n = 0;

    //set interval to 1 millisecond

    while (true) {

        printf ("New price...\n");

        //  Publish price and write to database

        SleepExecution (n);

    }

    return 0;
}

I have not been able to find an API call which will allow me to stop the execution of the above code for the arbitrary n milliseconds. Sleep looks to be the solution but I would prefer not to use it.

Are there any libraries you would recommend using or samples on the web I could draw inspiration from?


CreateWaitableTimer is great for running code periodically. Combined with timeBeginPeriod to increase the system timer rate, and turning up your thread priority so it wakes on time, you should have a solution that's 99.99% effective.


Suspending execution is an operating system-specific operation.

For MS Windows (Win 95 and after), use the function Sleep() where the parameter is the minimum rescheduling time in milliseconds.

For Linux, there are several ways, but int nanosleep(const struct timespec *req, struct timespec *rem); allows nanosecond precision for most uses. sleep() allows one second precision, so it probably isn't useful for your purposes.


I see that win32 was mentioned. To use Sleep(), the program can probably use a constant value, depending on the requirements, but if high consistency is required, then dynamically compute the delay based on how much longer it is until the next time a update is needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜