Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 301059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:58:15+00:00 2026-05-12T06:58:15+00:00

Can I do any of the following? Will they properly lock/unlock the same object?

  • 0

Can I do any of the following? Will they properly lock/unlock the same object? Why or why not? Assume there are many identical threads using global variable “obj”, which was initialized before all threads started.

1.

@synchronized(obj) {
    [obj release];
    obj = nil;
}

2.

@synchronized(obj) {
    obj = [[NSObject new] autorelease];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:58:16+00:00Added an answer on May 12, 2026 at 6:58 am

    Short answer: no, they won’t properly lock/unlock, and such approaches should be avoided.

    My first question is why you’d want to do something like this, since these approaches nullify the purposes and benefits of using a @synchronized block in the first place.

    In your second example, once a thread changes the value of obj, every subsequent thread that reaches the @synchronized block will synchronize on the new object, not the original object. For N threads, you’d be explicitly creating N autoreleased objects, and the runtime may create up to N recursive locks associated with those objects. Swapping out the object on which you synchronize within the critical section is a fundamental no-no of thread-safe concurrency. Don’t do it. Ever. If multiple threads can safely access a block concurrently, just omit the @synchronized entirely.

    In your first example, the results may be undefined, and certainly not what you want, either. If the runtime only uses the object pointer to find the associated lock, the code may run fine, but synchronizing on nil has no perceptible effect in my simple tests, so again you’re using @synchronized in a pointless way, since it offers no protection whatsoever.

    I’m honestly not trying to be harsh, since I figure you’re probably just curious about the construct. I’m just wording this strongly to (hopefully) prevent you and others from writing code that is fatally flawed, especially if under the assumption that it synchronizes properly. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question 1 Can anyone tell me if there is any difference between following 2
Is there any tools for replication in postgres which can doing the following :
Can any one tell me how to solve the following scenario in Asp.net application.
Can any body help me to convert the following SQL query in LinqToSql syntax.
Can any one help me how to sort rows by relevance for the following
Can any one tell me? what does following means in ruby program: obj =
In C++ one can write any of the following statements: 10; true; someConstant; //if
I have a URL which can be any of the following formats: http://example.com https://example.com
Any suggestions on how I can cleanup the following code pattern that repeats multiple
I have an example project, a dynamic questionnaire system where any administrator can create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.