Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Two Newbie Questions for C++?

This topic is 3651 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

Is proper to use breaks in loops? for(i = 0; i < 100; i++) { if(SomeA == true) break; } Do variables need mutexs? Thread 1 Mutex.Lock if(SomeA == true) doing something Mutex.Unlock Thread 2 Mutex.Lock if(SomeB == true) SomeA = true; Mutex.Unlock Sorry if these are basic questions. I'm looking to learn correct form and style. I know the code would "work." I'm looking to create create code. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
You can use breaks in loops, but if the test fits more naturally in the loop condition, then try put it there. Be careful with nested loops, you only break out of the current loop.

No variable needs a mutex. However, if you are working with multi-threaded code then you may need to use them. It depends heavily with what you are doing. If you are writing multi-threaded code - it usually looks nothing like the single threaded version of the same code. Mutli-threaded code + excessive locks = Single-threaded code (and worse performance, typically).

If you are not very experienced, I would recommend staying away from multi-threading for the moment.

Share this post


Link to post
Share on other sites
Quote:
Original post by fathom88
Is proper to use breaks in loops?

Sometimes.

Quote:
Do variables need mutexs?

Sometimes.

Quote:
Sorry if these are basic questions. I'm looking to learn correct form and style. I know the code would "work."

Code is something that tries to solve a given problem correctly. But without context, such rules are next to impossible to set.

Share this post


Link to post
Share on other sites
Quote:
Original post by fathom88
I'm looking to learn correct form and style.


I don't think this addresses your specific questions, but if you're on the subject try this.

http://www.parashift.com/c++-faq-lite/

Share this post


Link to post
Share on other sites
1° The original intent of break was to allow leaving a loop. Some people explain that break should never be used in a loop, because it is more difficult to work with the loop on an algorithmic scale if it has more than one way to terminate. This, however, is an extreme point of view.

The choice of using break or not is yours. The basic idea is that your code should be structured so that your loop fits on a single page of code, thereby making the entire loop structure (including any breaks) obvious to the reader. A loop body that is longer than a page of code is a bad idea, regardless of whether it contains early-outs or not.

Last but not least, always try to determine if the loop could not be written without break: if an elegant solution exists, you should be using it. break, like any other control structure, should only be used when it is the most elegant solution to the problem.

2° Asynchronous access to data is too complex of a task to be left to random parts of your program. Regardless of what you are doing, the process which involves communicating between two threads by means of a piece of data should be encapsulated in a class. That class will then use a mutex or other primitive to synchronize the messages sent by all threads using it.

In that philosophy, variables do not need mutexes, but classes meant to be shared by threads need mutexes to synchronize access to their members.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
break, like any other control structure, should only be used when it is the most elegant solution to the problem.

Well said!

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!