Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo,

I would like to create 4 threads, each thread has own multimedia timer.
I use these code
C++
#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

//struch for each Core
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();

//use for convert tick to ns
long double factor = 0;

//data storage in each core
process core[4];

//use for Timer resolution
TIMECAPS tc;
UINT wTimerRes;


VOID CALLBACK TimerRoutine(BOOLEAN TimerOrWaitFired,int arg)
{
	printf("args : %d\n",arg);
	//printf("Routine[%d] cnt:%d\n",arg,core[arg].cnt);
	if(core[arg].cnt++ >=MAX_LOOP)
		SetEvent(core[arg].gDoneEvent);
	
}

int main()
{
	//variable for thread handler
	HANDLE hThread[EVENT_NUM]; 
    DWORD ThreadIDCore[EVENT_NUM]; 
	
	//set mmtimer Resolution
	setResolution();

	//create thread 
	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,	//timer buffer
				    core[arg].hTimerQueue,	//timer
				    //call back rutine
				    (WAITORTIMERCALLBACK)TimerRoutine, 
				(PVOID)arg ,		//arg passing
				TIME_BEFORE_START,	//time before start(ms)
				TMR_INTERVAL_MS,//timer interval
				0))	//default timer
    {
        printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
     }
	if (WaitForSingleObject(core[arg].gDoneEvent, INFINITE) != WAIT_OBJECT_0)
		printf("WaitForSingleObject failed (%d)\n", GetLastError());
	//close event handler
	CloseHandle(core[arg].gDoneEvent);
	//delete timer
	if (!DeleteTimerQueue(core[arg].hTimerQueue))
			printf("DeleteTimerQueue failed (%d)\n", GetLastError());
	printf("Thread[%d] Completed\n",arg);
    return 0;
}

/*
	must import winmm.lib in to project using
	Project > ProjectName Properties(Alt+F7)
	Configulation properties > Linker > Input > 
	additional dependency > add "winmm.lib;"
*/
void setResolution()
{
	//get timer resolution
	if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) 
	{
		printf("TimeGetDevCaps Error \n");
	}
	//get max resolution
	wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
	//set max resolution
	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.
Posted
Updated 12-Apr-12 2:54am
v2

1 solution

Question 1: Why does the program print a series of 1s?

It looks you defined TimerRoutine with the parameters in the wrong sequence. It should be:

VOID CALLBACK TimerRoutine (PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
    int arg = (int) lpParameter;
    ...
}


Hence you are receiving on your arg parameter always the TimerOrWaitFired argument, which in this case is always 1.

Question 2: mmtimer doesn't stop

First of all: The Multimedia Timer has nothing to do with the timers you used in your thread functions. Those are Windows timers and are not connected to the multimedia system. So you can remove your setResultion functions and the example should still run.

Threads 2 - 4 will never stop, because their counters are not incremented, because of the reason shown in question 1 above (arg is always 1). As the threads never stop the WaitForMultipleObjects will wait forever ...

So by fixing problem 1 everything should work fine also.
 
Share this answer
 
Comments
windyl3ig 12-Apr-12 16:50pm    
Very thank you for your guideline.

That is my mistake. very thank you to collect it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900