Click here to Skip to main content
13,591,466 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 25 Mar 2005

Low resolution timer class for Visual C++ and GNU C++ under MingW

, 9 Jan 2006
Rate this:
Please Sign up or sign in to vote.
LRTimer - universal low resolution timer class with its own thread.


I was writing a small project for a friend of mine that needed a simple low-resolution timer to be used to call a function in equal time intervals as you usually do. When I Googled for timer classes, I found some code in C with __stdcall static linking, and since I prefer all my code to be encapsulated I decided to write my own timer class that will take any callback function instead.

The LRTimer class can be used in GNU C++/MingW and Visual C++ 6.0 projects without any changes to the source code. The example project in demo package has been compiled with Bloodshed Dev-C++

The challenge

The biggest problem I came about was how to use the class member function as a callback parameter for CreateThread and SetWaitableTimer API calls. Both the calls require that you provide a static callback C-type function reference.

I have found a good solution by Daniel Lohmann in his article "Use member functions for C-style callbacks and threads - a general solution" at the CodeProject and swiftly adopted it to my timer project.

Daniel uses an adapter <CODE lang=c++>static method as a wrapper to call non-static member functions. The idea behind adapter is to convert the _cdecl or _stdcall calls (C convention) into thiscall - a default call convention for class members. The conversion is necessary since thiscall passes additional parameters to your member function when calling it, namely <CODE lang=c++>this, and a callback function usually requires specified number of arguments. Compiler first adds an additional parameter to arglist and then tries to match the number or arguments for that call - great! It is also worth noting that if you want to be able to use non-static class variables within a callback method you cannot use static callback functions by default.

timerThread is the non-static callback function that will be wrapped and passed to the CreateThread API call to create new threads when timer.start() is called.

// timer clocking thread runtine
 virtual DWORD WINAPI timerThread();
// wrapper to thread runtine so it can be used within a class
 static DWORD WINAPI timerThreadAdapter(PVOID _this) {
   return ((LRTimer*) _this)->timerThread();

Note that timerThread takes no parameters and timerTreadAdapter takes one (void *) - placeholder for <CODE lang=c++>this. <CODE lang=c++>this is then cast to the class pointer (LRTimer *) and is used to call non-static timerThread- neat solution! If you now look at how timerThreadAdapter is called you will notice that <CODE lang=c++>this is passed explicitly as its argument:

m_hTimerThread = CreateThread(NULL, 0, timerThreadAdapter, this ,0,&m_iID);

Download the source code to see how all this is done. That is all I wanted to share with you today.

Please feel free to use the LRTimer class in your own projects and I would appreciate if you drop me a line about how you've used it.

Happy coding!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Systems Engineer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralLow resolution timer class for Visual C++ and GNU C++ under MingW Pin
Andrioindonesia16-Sep-06 22:44
memberAndrioindonesia16-Sep-06 22:44 
Dear Programmer,
Here,Let me to introduce my self
My name is Andrio,From Indonesia.
I'm newbie to Programming
I don't know about this command and this code about Low resolution timer class for Visual C++ and GNU C++ under MingW.
I would like to know how to using and running the code.
i'm using Slackware and XP sp2 OS
I don't know,how i can write the code on my Operating system and How to running the code.
Please for help me?
I need LIVE CD for practice at my home.

Thank you


Indonesian,yogyakarta beach-Indonesia country

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180618.1 | Last Updated 9 Jan 2006
Article Copyright 2005 by MaxOfLondon
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid