title: is there a way that the synchronized keyword doesn't block the main thread
body: <p>Imagine you want to do many thing in the background of an iOS application but you code it properly so that you create threads (for example using GCD) do execute this background activity.</p>
  
  <p>Now what if you need at some point to write update a variable but this update can occur or any of the threads you created.</p>
  
  <p>You obviously want to protect that variable and you can use the keyword <code>@synchronized</code> to create the locks for you but here is the catch (extract from the Apple documentation)</p>
  
  <blockquote>
    <p>The <code>@synchronized()</code> directive locks a section of code for use by a
    single thread. Other threads are blocked until the thread exits the
    protected code—that is, when execution continues past the last
    statement in the <code>@synchronized()</code> block.</p>
  </blockquote>
  
  <p>So that means if you synchronized an object and two threads are writing it at the same time, even the main thread will block until both threads are done writing their data.</p>
  
  <p>An example of code that will showcase all this:</p>
  
  <pre><code>// Create the background queue
  dispatch_queue_t queue = dispatch_queue_create("synchronized_example", NULL);
  
  // Start working in new thread
  dispatch_async(queue, ^
                 {                   
                     // Synchronized that shared resource
                     @synchronized(sharedResource_)
                     {   
                         // Write things on that resource
                         // If more that one thread access this piece of code:
                         // all threads (even main thread) will block until task is completed.
                         [self writeComplexDataOnLocalFile];
                     }                         
                 });
  
  // won’t actually go away until queue is empty
  dispatch_release(queue);
  </code></pre>
  
  <p>So the question is fairly simple: How to overcome this ? How can we securely add a locks on all the threads EXCEPT the main thread which, we know, doesn't need to be blocked in that case ?</p>
  
  <p><strong>EDIT FOR CLARIFICATION</strong></p>
  
  <p>As you some of you commented, it does seem logical (and this was clearly what I thought at first when using synchronized) that only two the threads that are trying to acquire the lock should block until they are both done.</p>
  
  <p>However, tested in a real situation, this doesn't seem to be the case and the main thread seems to also suffer from the lock. </p>
  
  <p>I use this mechanism to log things in separate threads so that the UI is not blocked. But when I do intense logging, the UI (main thread) is clearly highly impacted (scrolling is not as smooth). </p>
  
  <p>So two options here: Either the background tasks are too heavy that even the main thread gets impacted (which I doubt), or the synchronized also blocks the main thread while performing the lock operations (which I'm starting reconsidering).</p>
  
  <p>I'll dig a little further using the Time Profiler.</p>