Hallo,
I would like to create 4 threads, each thread has own multimedia timer.
I use these code
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <process.h>
#include <fstream>
#include <iostream>
#include <math.h>
#define EVENT_NUM 4
#define TARGET_RESOLUTION 1
#define MAX_ELEMENT 1000000
#define TMR_INTERVAL_MS 1
#define MAX_LOOP 5
#define TIME_BEFORE_START 10
typedef struct {
HANDLE gDoneEvent;
HANDLE hTimer ;
HANDLE hTimerQueue;
int cnt;
}process;
DWORD WINAPI ThreadRunAtCoreX( int arg );
int myPow(double base,int exp);
void setResolution();
unsigned __int64 getFreq();
long double factor = 0;
process core[4];
TIMECAPS tc;
UINT wTimerRes;
VOID CALLBACK TimerRoutine(BOOLEAN TimerOrWaitFired,int arg)
{
printf("args : %d\n",arg);
if(core[arg].cnt++ >=MAX_LOOP)
SetEvent(core[arg].gDoneEvent);
}
int main()
{
HANDLE hThread[EVENT_NUM];
DWORD ThreadIDCore[EVENT_NUM];
setResolution();
for(int i=0;i<EVENT_NUM;i++)
{
hThread[i] = CreateThread( NULL, 0,
(LPTHREAD_START_ROUTINE)ThreadRunAtCoreX,
(LPVOID )i,
0,
&ThreadIDCore[i]);
}
if(WaitForMultipleObjects(EVENT_NUM,hThread,TRUE,INFINITE)==WAIT_FAILED)
printf("Waiting failed, error %d...\n", GetLastError());
printf("All thread finished\n");
for(int i=0;i<EVENT_NUM;i++)
{
if(CloseHandle(hThread[i])!=0)
{
printf("Close thread[i] successfull\n",i);
}
else
{
printf("Fail to close thread[i], Error(%u)\n",GetLastError());
}
}
getchar();
return 0;
}
DWORD WINAPI ThreadRunAtCoreX( int arg )
{
core[arg].gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
core[arg].hTimerQueue = CreateTimerQueue();
if (!CreateTimerQueueTimer( &core[arg].hTimer, core[arg].hTimerQueue, (WAITORTIMERCALLBACK)TimerRoutine,
(PVOID)arg , TIME_BEFORE_START, TMR_INTERVAL_MS, 0)) {
printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
}
if (WaitForSingleObject(core[arg].gDoneEvent, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
CloseHandle(core[arg].gDoneEvent);
if (!DeleteTimerQueue(core[arg].hTimerQueue))
printf("DeleteTimerQueue failed (%d)\n", GetLastError());
printf("Thread[%d] Completed\n",arg);
return 0;
}
void setResolution()
{
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR)
{
printf("TimeGetDevCaps Error \n");
}
wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes);
}
I should get number 1,2,3,4's 5 times each.
but I get always "non-stop 1's"
my question is :
1. Why dose it be?, I set and closed handler for mmtimer but it doesn't stop.
2. How could I solve it?
very thank you for every comment and guideline.