Can I use string as locker in the Lock ?
lock("something")
Can I do lock without braces if its only one line ?
lock("something") foo();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
1) Yes, strings are (generally) interned (by default, thanks @Eric), so any instance of the same
"something"would point to the same object, therefore you’d be ok. This is very bad practice though, because someone else, in another library for example, could lock on your string, thus giving the potential for deadlocks. See here: Using string as a lock to do thread synchronizationYou should do this:
2) Yes, same with all statements. Anything* where you have:
could just be
*Almost anything, see @LukeH’s example of the
catchblock, which requires the braces.