Sign in to follow this  
Floating

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

Recommended Posts

Floating    122
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
Floating    122
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
JohnBolton    1372
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
Floating    122
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
Floating    122
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
JohnBolton    1372
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this