Link to an example of a windows multi-threaded dos console program to copy a file, one thread reads, the other writes, using Microsoft windows multithreading objects (mutexes, semaphores), and functions as opposed to generic ones. It uses a custom messaging system to communicate between threads. The code in main() to set this up is a bit tedious, but the actual functions themselves are fairly simple.
http://rcgldr.net/misc/mtcopy.zip
For a fixed frequency loop within a thread, reading a high frequency counter along with using Sleep(1) (up to 2ms delay in the case of Win XP) to keep from going cpu bound is useful. Example code:
Code:
typedef unsigned long long UI64; /* unsigned 64 bit int */
#define FREQ 400 /* desired frequency rate */
/* ... */
LARGE_INTEGER liPerfFreq; /* 64 bit frequency */
LARGE_INTEGER liPerfTemp; /* used for query */
UI64 uFreq = FREQ; /* process frequency */
UI64 uOrig; /* original tick */
UI64 uWait; /* tick rate / freq */
UI64 uRem = 0; /* tick rate % freq */
UI64 uPrev; /* previous tick based on original tick */
UI64 uDelta; /* current tick - previous */
UI64 u2ms; /* 2ms of ticks */
UI64 i;
/* ... */
QueryPerformanceFrequency(&liPerfFreq);
u2ms = ((UI64)(liPerfFreq.QuadPart)+499) / ((UI64)500);
timeBeginPeriod(1); /* set period to 1ms */
Sleep(128); /* wait for it to stabilize */
while(1){
/* ... fixed frequency code goes here ... */
while(1){ /* delay loop */
QueryPerformanceCounter(&liPerfTemp);
uDelta = (UI64)(liPerfTemp.QuadPart - uPrev);
if(uDelta >= uWait)
break;
if((uWait - uDelta) > u2ms)
Sleep(1);
}
uPrev += uWait;
/* ... code to break out of loop goes here ... */
}
timeEndPeriod(1); /* restore period */
/* ... */