Arne
Arne

Reputation: 1151

Portable periodic timer for period around 100ms

Hej!

I am looking for a portable way of periodically dispatching a task in a C++ project. The use of libraries such as boost should be avoided in this particular project.

The resolution requirement is not too serious: between 5Hz to 20Hz on an average Netbook.

The project uses OpenGL to render the HMI but since I am working on the backend part I am not too familiar with it.

Thanks your any advice or suggestions, Arne

EDIT: What our 'Task' class actually does is creating a thread using either CreateThread(..) for windows or pthread_create(..) for linux.

Upvotes: 2

Views: 1448

Answers (4)

Oleg Zhylin
Oleg Zhylin

Reputation: 1310

As most straightforward way to achieve this is to use Sleep(100ms) in a cycle, all you need is a portable Sleep. For Linux it can be implemented as follows

void Sleep(unsigned long ulMilliseconds)
{
    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = ulMilliseconds * 1000;
    select(1, NULL, NULL, NULL, &timeout);
} 

Upvotes: 1

stefaanv
stefaanv

Reputation: 14392

If you want a periodic trigger, a thread that sleeps for 100ms in a loop might do the trick.

Upvotes: 1

sharptooth
sharptooth

Reputation: 170469

Since you already know the complete set of target systems you can go the SQLite way. They have exactly the same problem - many things they use are system-dependent.

All system-dependent things are implemented separately for every target system and the right version is compiled depending on preprocessor directives set.

Upvotes: 1

neuro
neuro

Reputation: 15180

Well I think you can find a lot of libraries that include this sort of thing, like Glibmm/sigc++, but I think that's too big a hammer for your nail. As I suppose you already have libraries to work with, if they include portable multithreading and no periodical timer you can design a simple way to do that by yourself. If the timing requirement is low, just start a thread that run an infinite loop that sleeps and calls your callback method when it wakes up. You can use sigc++ to handle your callbacks as it is lightweight. If you do not have threads and such and do not want to use boost, you can give Glibmm a try. but If you don't want boost I suppose you will not want such a big library neither :-)

my2cents.

Upvotes: 0

Related Questions