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 6823345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:46:18+00:00 2026-05-26T21:46:18+00:00

There is one thing I am not sure about when it comes to locks.

  • 0

There is one thing I am not sure about when it comes to locks. I’ve read http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW16 but one thing I am not sure about; does @synchronize (or just mutex in general) protect only a section of code (say inside a method) or lock the entire object as a whole?

For example, two threads working on these methods, which modify an array;

@synthesize m_myMutableArray;

-(void)threadA
{
    @synchronized(m_myMutableArray) {
         [m_myMutableArray removeAllObjects];
    }
}

-(void)threadB
{
    NSInteger asdf = 1;
    @synchronized(m_myMutableArray) {
         [m_myMutableArray addObject:asdf];

}

Does @synchronized not do anything because they are two separate blocks of code, or is it the same mutex being locked in both methods, meaning m_myMutableArray is threadsafe?

Thanks

  • 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-26T21:46:19+00:00Added an answer on May 26, 2026 at 9:46 pm

    The “argument” to @synchronized is a so-called token or key so you can have different locked sections. They only block each other when they have the same token. The object themselves are not “locked”.

    So if you have two @synchronized(foo) and two @synchronized(bar), the foo sections block each other but will not block the bar sections.

    If possible, you should avoid @synchronized as it’s very slow, due to its dynamic nature.

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

Sidebar

Related Questions

There is one thing that I do not understand... Imagine you have a text
There's one thing I haven't found in RFC 2616 ("Hypertext Transfer Protocol -- HTTP/1.1")
There is one thing I really love about LXML, and that the E builder.
I am learning Java, theres one thing I do not understand.. in the main
Defining custom components in Facelets is easy and quick but there's one thing I
I'm fairly new to programming, and there's one thing I'm confused by. What is
I've been working with pthreads a fair bit recently and there's one little thing
So there's the Do One Thing rule in the book Clean Code. But how
One thing I've noticed with all the XML-RPC examples out there, including the spec
there is one think, i can't understand anyway:((( when i try to set cookie(it

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.