Windows multithreading?

Started by
4 comments, last by Architekt 22 years, 3 months ago
Perhaps I''m confusing terminology..if so someone please correct me. Anyways, I''ve got a mouse update/polling function that I''d ideally like to be automatically called every certain number of times per second (roughly of course). I had used the Windows SDK CreateThread method to try and get that to work (I''m skipping over the non important stuff here), and it just gets called once and then exits. That makes sense to me, cause it''s just starting a new thread, which isn''t supposed to be called again after it finishes execution. But what I can''t figure out is how to get my update/polling function to be run in a separate thread AND be called repeatedly a certain number of times per second. Can someone explain or at least point me to the right MSDN page? Thanks.
Advertisement
I don''t have time to go into detail now, but why not make the function your thread runs contain a simple loop?

  // pseudo codeint someFunction(){  while(gameIsRunning)  {    if(currenttime==nextTime)    {      pollInput();      nextTime=currenttime+100;    }  }  return 0;}  


Basically, just keep looping through as long as the game is still running. Get the time, and if it''s been a certain inverval (say 100 milliseconds) since the last time you polled, do it again, then set the next time to try to 100ms in the future.

Make sense? Someone will probably give a better answer later on anyway heh.

Anthracks
The function you pass to CreateThread should sit in a loop and poll the mouse. To terminate it, you can send it a message (SendThreadMessage) or use a Mutex.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Dont forget to Sleep(10) in that thread or you''ll be disapointed
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Don''t forget that if you use CreateThread you cannot use the C runtime library. Use beginthread instead.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Check your SDK both WIN32 and DX (if u are using it) for timer functions. Once you setup the timer, it sends your app messages handled by your window message procedure(WM_TIMER). From here you could either:
1.Don't use threads at all, Poll using a procedure, it being the WM_TIMER message handler. (No extra overhead since u have to deal with the message loop.)
2.In the WM_TIMER message handler create/destroy/sleep/wake.. your "input thread".
3.Same as one but put your rendering and processor intensive code into a seperate lower priority thread (not that much lower than your main/input thread). This will give you best results performance and quality wise.

-potential energy is easily made kinetic-

Edited by - Infinisearch on January 13, 2002 1:15:45 PM

-potential energy is easily made kinetic-

This topic is closed to new replies.

Advertisement