Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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)
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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
}
}
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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
}
}



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???
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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.

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..
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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.

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..
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
But its retruning a "nil" , isnt it?
No, as this gets set in allocWithZone which gets called from alloc.

So i changed it to

Code:
sharedGizmoManager = [[self alloc]init];

(even in doc also its mentioned that assignment not done there , then where?
Change it back and implement allocWithZone. The document does all of these things for a reason.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.