Boost logo

Boost :

From: William E. Kempf (wekempf_at_[hidden])
Date: 2003-06-30 15:47:50


Maxim Egorushkin said:
>
> "William E. Kempf" <wekempf_at_[hidden]> wrote in message
> news:1502.167.16.75.51.1056996926.squirrel_at_frodo.kempf-ville.com...
>
>> > Speaking about the timer I ment something like that:
>> >
>> > typedef int milliseconds;
>> >
>> > class stopwatch
>> > {
>> > public:
>> > stopwatch()
>> > : started_(::GetTickCount())
>> > {}
>> >
>> > milliseconds elapsed() const
>> > {
>> > return ::GetTickCount() - started_;
>> > }
>> >
>> > private:
>> > const DWORD started_;
>> > };
>>
>> Ahh... that's not a threading concept ;).
>
> Let me disagree here :) A couple of days ago I was implementing a user
> mode task scheduler. And I had the scheduler thread updating the tasks
> delays 4 times per second and putting ready for execution tasks in the
> execution queue. I tryed to make it portable but the problem was that I
> could be sure that the scheduler thread would receive its time slice
> exactly every 250 ms. To solve the problem I decided to increase the
> scheduler thread priority and to measure the time the thread spent
> sleeping till the next time slice. I was using boost::thread library and
> my solution could be implemented by means of the library and made my
> code unportable. That was the rationale of my posting.

I'm way confused here. The code you have above simply tracks elapsed
time. This is in no way thread specific, and Boost already has such a
library.

Now, what you're describing sounds more like this:

class timer
{
public:
   timer(boost::function<void> on_event, int ms, bool repeat=false);
};

Which do you really want?

As for "the problem was that I could be sure that the scheduler thread
would receive its time slice exactly every 250 ms", you simply can't do
this, portably or not. Granularity issues of the underlying clock aside,
I'm not aware of any scheduler that would give you this sort of control,
and fiddling with the priorities will only give you the illusion that
you've accomplished what you want.

-- 
William E. Kempf

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk