views:

302

answers:

4

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.

A: 

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.
neuro
+1  A: 

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.

sharptooth
Thank you. Indeed the #define controlled compilation exactly what is happening at the moment. What I am looking for is kind of a "hidden" API that allows to set a period of a thread in both target systems. I am afraid I am forced roll out my own solution...
Arne
Very portable way, indeed...
Piotr Dobrogost
What do you mean by "hidden"?
sharptooth
@sharptooth Hidden in the sense "not obvious from the API documentation on MSDN"
Arne
+1  A: 

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

stefaanv
+1  A: 

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);
}
Oleg Zhylin