Jump to content

C++ Multithreads Timers

- - - - -

  • Please log in to reply
7 replies to this topic

#1
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Hey im making a program that uses multiple timers that finish at different times. Im using threads although if there is any other ways im happy to hear them. So far I have:


void Thread( void* pParams )

{  

	

}


void Timer::StartTimer()

{	

	Timer *t1 = new Timer (1, 4500);

	Timer *t2 = new Timer (2, 6000);

	Timer *t3 = new Timer (4, 3000);

	

}


Timer::Timer()

{}


Timer::Timer (int zoneIn, int timeIn):zone(zoneIn),time(timeIn)

{

	_beginthread( Thread, 0, NULL );

	cout<<"start "<<endl;

	//Sleep(time);

	cout<<"end "<<endl;

}

Obviously i am missing code but all the thread stuff is there
the sleep command works, it pauses for 3 seconds the printing end. but its not multithreading.
thanks

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

From you explanation I understand(if I've not miss-understood you) that you probably want to simulate multi-thread environment: you need to add some code into Thread function to be executed when you create a thread through _beingThread: Try following


void Thread( void* pParams )

{  

        int *milliseconds = (int *)pParams;

	Sleep(milliseconds);

       cout<<"Thread finished after "<<milliseconds<<" ms"<<endl;


}


Timer::Timer (int zoneIn, int timeIn):zone(zoneIn),time(timeIn)

{

	cout<<"creating a thread "<<endl;

	_beginthread( Thread, &time, NULL );

	cout<<"thread creation done "<<endl;

}



I hope this helps!

Munir

#3
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Yeah im trying to use multithreading. When I use your code I get 2 errors
int *milliseconds = (int *)pParams;

	Sleep(milliseconds);
int * is not compatible with parameter type "DWORD"
and
_beginthread( Thread, [B]&time[/B], NULL );
From what I know about C++ & means its a reference to the variable 'time' but thats as far as my knowledge goes.

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

sorry, I made a mistake: the correct code is


_beginthread( Thread, 0, (void *) &time);


I hope this fixes the problem!

Munir

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts

int *milliseconds = (int *)pParams;

	Sleep(milliseconds);


The above should be

DWORD milliseconds = *(DWORD *)pParams;

	Sleep(milliseconds);


or simply this
Sleep(*(DWORD*)pParams);
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#6
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
That solved my problem! Thank you Munir and Ancient Dragon your help is appreciated.
Just another question to help me better understand, this code:
_beginthread( Thread, 0, (void *) &time);
starts a thread. Its parameters Thread is the name if thread starting? What do other parameters mean, plus are there any more that can be included?

#7
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
HI,


_beginthread( Thread, 0, (void *) &time);


Thread is the function pointer that created thread woulc execute:
&time represent pointer that would be passed to the routine Thread(void *args) as function argument.

I hope this helps!

Munir

#8
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Ok think i got a better understanding of it now
Thanks again for your help :c-grin:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users