• Advertisement
Sign in to follow this  

Main loop, threads, and so on... How?

This topic is 4853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have an openGL application that renders once in a main loop. The main loops also contains the messaging functions. It basically looks like: bool CMyApp::mainLoop() { while (true) { MSG message; while(getWindowMessage(message)) processWindowMessage(message); ... other things renderScene(); } } My question is: if I run another thread next to the main one, will the thread be processed automatically every x miliseconds, or will it depends on how long the function "renderScene()" takes? I tried to set-up a communication thread (sockets) for my application but that makes my whole application much slowlier!! I have no idea why this is happening Thanks for your help :)

Share this post


Link to post
Share on other sites
Advertisement
Isn't a thread similar to a second application running in parallel to the main application?
My communication thread's loop needs to be run every 10-30 miliseconds approximately while the loop of my main application will be run every 50-200 miliseconds (depending on how complex the openGL scene is)

Share this post


Link to post
Share on other sites
In windows, if you create another thread with the same priority, the two threads will take turns. Each thread will run until it uses up or gives up its alloted time. When that happens the other thread starts running.

If you create another thread and it never gives up any of its time (by waiting for I/O or Sleep), then your original thread will run at half speed.

As a side note, here is a quick question about your pseudocode. What will happen if your application generates/receives two messages every frame?

Share this post


Link to post
Share on other sites
Thank you for your explanation JohnBolton!

If my application receives 2 (or more) messages every frame, the messages are processed until the message pipe is empty, then rendering is performed again

Share this post


Link to post
Share on other sites
BTW, how often is Windows switching between threads (if both threads are all the time active (not sleeping))?
Is it less than 10 miliseconds?

Share this post


Link to post
Share on other sites
Another quick question:

If I access some data in thread1 and do not want thread2 to access the data at the same time, what command should I use for "protected access"?

Share this post


Link to post
Share on other sites
Quote:
Original post by Floating
Another quick question:

If I access some data in thread1 and do not want thread2 to access the data at the same time, what command should I use for "protected access"?


Look up "CRITICAL_SECTION".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement