Click here to Skip to main content
15,903,362 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: threads and their time slots. Pin
Keith Worden3-Jul-09 5:29
Keith Worden3-Jul-09 5:29 
GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:04
Souldrift3-Jul-09 6:04 
AnswerRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:07
Souldrift3-Jul-09 6:07 
GeneralRe: threads and their time slots. Pin
Keith Worden3-Jul-09 6:18
Keith Worden3-Jul-09 6:18 
GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:39
Souldrift3-Jul-09 6:39 
AnswerRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:48
Souldrift3-Jul-09 6:48 
AnswerRe: threads and their time slots. Pin
Souldrift3-Jul-09 7:34
Souldrift3-Jul-09 7:34 
GeneralRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 8:33
professionalStuart Dootson3-Jul-09 8:33 
Souldrift wrote:
And so on ... it always jumps 15,6ms forward


Here's what Mark Russinovitch and David Solomon say in Windows Internals

On Windows 2000 Professional and Windows XP, threads run by default for 2 clock intervals; on Windows Server systems, by default, a thread runs for 12 clock intervals. The rationale for the longer default value on server systems is to minimize context switching. By having a longer quantum, server applications that wake up as the result of a client request have a better chance of completing the request and going back into a wait state before their quantum ends


The length of the clock interval varies according to the hardware platform. The frequency of the clock interrupts is up to the HAL, not the kernel. For example, the clock interval for most x86 uniprocessors is about 10 milliseconds and for most x86 multiprocessors it is about 15 milliseconds. (The actual clock rate is not exactly a round number of milliseconds—see the following experiment for a way to check the actual clock interval.)


EXPERIMENT: Determining the Clock Interval Frequency


The Windows GetSystemTimeAdjustment function returns the clock interval. To determine the clock interval, download and run the Clockres program[^] from http://www.sysinternals.com.




That kind of confirms what you're seeing (and what I see - which is 15.625, or 500/32).

HOWEVER!!!! I steered you wrong with waitable timers. I mistook them for a TimerQueueTimer in my memory. Try this:

#include <Windows.h>
#include <tchar.h>
#include <iostream>

void ReportTime(const char* message, LONGLONG const& when)
{
   std::cout << message << double(when)/10000.0 << std::endl;
}

LONG count;
LARGE_INTEGER liStart, liEnd, liFreq;

VOID CALLBACK DoSendHere(__in_opt  LPVOID lpArgToCompletionRoutine,
                         __in      DWORD dwTimerLowValue,
                         __in      DWORD dwTimerHighValue)
{
   QueryPerformanceCounter(&liEnd);
   count++;
}

void SendLoop()
{
   HANDLE hTimerQueue = CreateTimerQueue();
   HANDLE hTimer;
   count = 0;
   CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)&DoSendHere, 0 , 1, 1, WT_EXECUTEINTIMERTHREAD);
   QueryPerformanceCounter(&liStart);
   ::SleepEx(65, TRUE);
   LONG lCount = count;
   QueryPerformanceFrequency(&liFreq);
   DeleteTimerQueueTimer(hTimerQueue, hTimer, 0);
   std::cout << "Time = " << double(liEnd.QuadPart-liStart.QuadPart) / double(liFreq.QuadPart) << std::endl;
   std::cout << "count = " << lCount << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
   SendLoop();
   return 0;
}


QueryPerformanceCounter lets you see much higher resolution time than other Windows timers (processor clock speed, effectively), so it's good for observing small time periods.
The CreateTimerQueueTimer call is creating a timer that has a 1ms period. So to demonstrate the number of timer expirations in a time period, I increment a counter in the timer callback and measure the time spent counting with the performance counter.

And the results? Consistently within ±40µs of countms.

Schweet.

Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: threads and their time slots. Pin
Roger Stoltz3-Jul-09 8:41
Roger Stoltz3-Jul-09 8:41 
GeneralRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 8:50
professionalStuart Dootson3-Jul-09 8:50 
GeneralRe: threads and their time slots. Pin
Roger Stoltz3-Jul-09 8:55
Roger Stoltz3-Jul-09 8:55 
GeneralRe: threads and their time slots. Pin
Souldrift5-Jul-09 21:37
Souldrift5-Jul-09 21:37 
GeneralRe: threads and their time slots. Pin
Stuart Dootson5-Jul-09 21:43
professionalStuart Dootson5-Jul-09 21:43 
GeneralRe: threads and their time slots. [modified] Pin
Souldrift5-Jul-09 22:19
Souldrift5-Jul-09 22:19 
GeneralRe: threads and their time slots. Pin
Stuart Dootson5-Jul-09 23:30
professionalStuart Dootson5-Jul-09 23:30 
AnswerRe: threads and their time slots. Pin
Roger Stoltz3-Jul-09 8:39
Roger Stoltz3-Jul-09 8:39 
QuestionHow to make truncation in C++ Pin
alikalik2-Jul-09 22:06
alikalik2-Jul-09 22:06 
AnswerRe: How to make truncation in C++ Pin
Rajesh R Subramanian2-Jul-09 22:09
professionalRajesh R Subramanian2-Jul-09 22:09 
GeneralRe: How to make truncation in C++ Pin
alikalik2-Jul-09 22:14
alikalik2-Jul-09 22:14 
GeneralRe: How to make truncation in C++ Pin
Rajesh R Subramanian2-Jul-09 22:16
professionalRajesh R Subramanian2-Jul-09 22:16 
AnswerRe: How to make truncation in C++ Pin
Stuart Dootson2-Jul-09 22:16
professionalStuart Dootson2-Jul-09 22:16 
AnswerRe: How to make truncation in C++ Pin
Michael Schubert2-Jul-09 22:19
Michael Schubert2-Jul-09 22:19 
GeneralRe: How to make truncation in C++ Pin
CPallini2-Jul-09 22:36
mveCPallini2-Jul-09 22:36 
GeneralRe: How to make truncation in C++ Pin
Rajesh R Subramanian2-Jul-09 22:50
professionalRajesh R Subramanian2-Jul-09 22:50 
GeneralRe: How to make truncation in C++ Pin
CPallini2-Jul-09 22:56
mveCPallini2-Jul-09 22:56 

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.