Compile error using @synchronized on methods that return values
Compile error using @synchronized on methods that return values
- Subject: Compile error using @synchronized on methods that return values
- From: Ken Tozier <email@hidden>
- Date: Sun, 8 Nov 2009 06:03:18 -0500
Hi
I wrote a MySQL connection class and have found that when I use the
accessors in multiple threads, the query results seem to be stomping
on each other. I looked into using @synchronized(self) to bracket the
calls to the lower level socket calls, I'm getting the following
warning:
"Control reaches end of non-void function"
I Googled for "Objective-C accessors and @synchrinized" which seemed
to indicate that the following was the correct way to handle it
- (id) someMethod
{
@synchronized(self)
{
return [somevalue autoreleae];
}
}
But I get the above error whenever I try to do it that way. Here's one
of the actual methods with and without the @synchronized directive
- (NSString *) directoryPathWithType:(NSString *) inDirectoryType
site:(NSString *) inSiteName
{
NSString *query = [NSString stringWithFormat: @"select id from
directory where name=' %@' and type='%@'", inSiteName, inDirectoryType];
id queryResult = [connection performQueryReturningOne: query];
return [[queryResult objectForKey: @"path"] value];
}
- (NSString *) directoryPathWithType:(NSString *) inDirectoryType
site:(NSString *) inSiteName
{
@synchronized(self)
{
NSString *query = [NSString stringWithFormat: @"elect id from
directory where name=' %@' and type='%@'", inSiteName, inDirectoryType];
id queryResult = [connection performQueryReturningOne: query];
return [[queryResult objectForKey: @"path"] value];
}
}
I understand why, under normal circumstances, that nesting would yield
the above mentioned compiler error, but I don't understand why every
example I've been able to find on the web says to do exactly that type
of @synchronized nesting. What is the correct way to make the above
function thread safe?
Thanks for any help
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden