PDA

View Full Version : Thread safe Static method




sujithkrishnan
Sep 24, 2008, 05:39 AM
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...
[ LogClass writeLog:string]

I want to make it thread safe... how???

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

method...

+(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
Sep 24, 2008, 05:50 AM
The excellent documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_12_section_1.html#//apple_ref/doc/uid/TP30001163-CH19-SW1), 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):


+ myThreadSafeMethod
{
@synchronized([MyClass class])
{
// Code here
}
}

sujithkrishnan
Sep 24, 2008, 08:21 AM
The excellent documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_12_section_1.html#//apple_ref/doc/uid/TP30001163-CH19-SW1), 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):


+ 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

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

return staticLoggerObject;
}
}


and i am writing logs using

[[Logger sharedLogger]writeToLog:myString];

is it ok???

robbieduncan
Sep 24, 2008, 08:25 AM
The code recommended by Apple for creating singleton objects is also in the documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html#//apple_ref/doc/uid/TP40002974-CH4-SW32). 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
Sep 25, 2008, 01:05 AM
The code recommended by Apple for creating singleton objects is also in the documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html#//apple_ref/doc/uid/TP40002974-CH4-SW32). 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.


+ (MyGizmoClass*)sharedManager
{
@synchronized(self) {
if (sharedGizmoManager == nil) {
[[self alloc] init]; // 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

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
Sep 25, 2008, 01:11 AM
The code recommended by Apple for creating singleton objects is also in the documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html#//apple_ref/doc/uid/TP40002974-CH4-SW32). 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.


+ (MyGizmoClass*)sharedManager
{
@synchronized(self) {
if (sharedGizmoManager == nil) {
[[self alloc] init]; // 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

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
Sep 25, 2008, 04:29 AM
But its retruning a "nil" , isnt it?
No, as this gets set in allocWithZone which gets called from alloc.

So i changed it to

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.