Re: Thread-safe singleton with lazy initialisation
Re: Thread-safe singleton with lazy initialisation
- Subject: Re: Thread-safe singleton with lazy initialisation
- From: Greg Parker <email@hidden>
- Date: Tue, 18 Aug 2015 13:25:44 -0700
> On Aug 18, 2015, at 9:29 AM, Maxthon Chan <email@hidden> wrote:
>
> Two questions:
>
> 1) How good will a Mac hold up as a server? I can serve static content from the CDN I rented but CGIKit code is dynamic stuff. If a Mac and OS X holds well, I can throw Linux compatibility out of the window and use GCD as I will.
> 2) If a Mac does not fare well as a server, I will have to make sure my code is compatible with GNUstep which is the best Cocoa clone for Linux I have encountered so far - complete with ARC and Blocks but GCD support on GNUstep is poor (and CoreFoundation support is spotty) so I have to avoid using GCD completely and use CoreFoundation only sparsely.
Apple's Objective-C runtime provides thread-safety guarantees for +initialize. If you are on some other platform then you should check if that platform provides similar guarantees.
dispatch_once() is the best solution on Apple's platforms.
If you don't like dispatch_once() then you could use pthread_once().
If you don't like dispatch_once() nor pthread_once() then you can use PTHREAD_MUTEX_INITIALIZER instead of pthread_mutex_init(). That avoids any questions of +initialize thread-safety.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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