Thread safe Static method

Discussion in 'iOS Programming' started by sujithkrishnan, Sep 24, 2008.

  1. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    Hi all..

    can anyone give a info/snippet of an implementation of a Thread safe static method...

    Details:

    I am having a method +(void)writeLog in class LogClass... In that i am opening a file for writing using NSFileHandle.

    But being a static method.. called in many classes, across thread...
    Code:
    [ LogClass writeLog:string]
    I want to make it thread safe... how???

    Can @synchronized help me??? But what paramaeter i pass for @synchronized??

    method...

    Code:
    +(void)writeLog:(NSString*)str
    {
    //  open file for writing using NSFileHandle.
    //  seek to end of file.
    // write the data
    // [filehandle closeFile];       (i removed this line as i am getting exception)
    }
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The excellent documentation, as is often the case, has the answer. Use @syncronized using the Class object.

    So it would look something like this (replace MyClass with the name of the class the method is in):

    Code:
    + myThreadSafeMethod
    {
    @synchronized([MyClass class])
    {
    // Code here
    }
    }
    
     
  3. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #3


    okie..thanks alot...

    But i tried another one...

    create a static method which return a static object..

    [Logger sharedLogger] // return me a staticloggerObject

    in that method i am writing

    Code:
    +(Logger*)sharedLogger
    { 
      @synchronized(staticLoggerObject)
      {
         if(staticLoggerObject == nil)
            staticLoggerObject = [[Logger]alloc]init];
    
         return staticLoggerObject;
      }
    }

    and i am writing logs using

    Code:
    [[Logger sharedLogger]writeToLog:myString];
    is it ok???
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    The code recommended by Apple for creating singleton objects is also in the documentation. I'd recommend using it (I do in my code)...

    Edit to add: and no, that's not really thread safe as 2 objects could get references to sharedLogger and call writeToLog at the same time: you still need to protect access to writeToLog if it's not thread safe.
     
  5. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #5
    Code:
    + (MyGizmoClass*)sharedManager
    {
        @synchronized(self) {
            if (sharedGizmoManager == nil) {
                [COLOR="Red"][[self alloc] init];[/COLOR] // assignment not done here
            }
        }
        return sharedGizmoManager;
    }
    
    I went thru this doc before also..

    But its retruning a "nil" , isnt it?
    So i changed it to

    Code:
    sharedGizmoManager = [[self alloc]init];
    (even in doc also its mentioned that assignment not done there , then where?

    Is it mandatory to implement allocWithZone as well?? Document in not sufficient enuf..
     
  6. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #6
    Code:
    + (MyGizmoClass*)sharedManager
    {
        @synchronized(self) {
            if (sharedGizmoManager == nil) {
                [COLOR="Red"][[self alloc] init];[/COLOR] // assignment not done here
            }
        }
        return sharedGizmoManager;
    }
    
    I went thru this doc before also..

    But its retruning a "nil" , isnt it?
    So i changed it to

    Code:
    sharedGizmoManager = [[self alloc]init];
    (even in doc also its mentioned that assignment not done there , then where?


    And moreover they are telling that override "release" with blank block..
    Can u tell me why?
    But if i am implementing like
    sharedGizmoManager = [[self alloc]init];

    I want to release that object , right?


    Is it mandatory to implement allocWithZone as well?? Document in not sufficient enuf in explanation, i think.. Hope u can help..
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    No, as this gets set in allocWithZone which gets called from alloc.

    Change it back and implement allocWithZone. The document does all of these things for a reason.
     

Share This Page