Re: Thread-safe singleton with lazy initialisation
Re: Thread-safe singleton with lazy initialisation
- Subject: Re: Thread-safe singleton with lazy initialisation
- From: Doug Hill <email@hidden>
- Date: Tue, 18 Aug 2015 11:41:47 -0700
A couple of things: you can
> 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.
>
>> On Aug 19, 2015, at 00:18, Simone Tellini <email@hidden <mailto:email@hidden>> wrote:
>>
>> Il giorno 18/ago/2015, alle ore 18:00, Maxthon Chan <email@hidden> ha scritto:
>>>
>>> So the first class that is required is the main application class CGIApplication. Being the analogue of UIApplication it is a singleton. Is this the proper way of doing it? I cannot use @synchronized yet because there is nothing to lock on:
>>
>> if you used GCD, it would be simpler:
>>
>> + (instancetype)sharedInstance
>> {
>> static dispatch_once_t once;
>> static id ret;
>>
>> dispatch_once( &once, ^{
>> ret = [[self alloc] init];
>> } );
>>
>> return ret;
>> }
>>
>> --
>> Simone Tellini
>> http://tellini.infoIs this the proper way to initialise a singleton object in a thread-safe manner?
> A little bit background, I am rewriting my CGIKit Web development framework for Objective-C and now Swift, and after the idea of building the Web application into a loadable bundle that either a FastCGI-speaking cgikitd or an Apache module mod_objc went up in smoke due to opening up serious security bugs and lifecycle management issues, I am going back to the path of building the Web application into an executable, now speaking FastCGI but not launched by the Web server.
>
> I am modelling the HTTP protocol layer (CGIKit) after ASP.net <http://asp.net/> and the Web layer (WebUIKit) after UIKit using bits and pieces from Bootstrap+jQuery (which itself is just a big CSS+JS file) as “controls”. However due to the fact that FastCGI event loop being part of the protocol some CGIApplication (analogue of UIApplication) have to be placed in CGIKit. UIKit’s non-atomic-ness is dropped and all operation have to be atomic, since Web servers are expected to process multiple requests at the same time.
>
> So the first class that is required is the main application class CGIApplication. Being the analogue of UIApplication it is a singleton. Is this the proper way of doing it? I cannot use @synchronized yet because there is nothing to lock on:
>
> #import <pthread.h>
>
> pthread_mutex_t _CGI_ApplicationStartingMutex;
>
> @implementation CGIApplication
>
> + (void)initialize
> {
> pthread_mutex_init(&_CGI_ApplicationStartingMutex, NULL);
> }
>
> + (instancetype)sharedApplication
> {
> if (!CGIApp)
> {
> pthread_mutex_lock(&_CGI_ApplicationStartingMutex);
> if (!CGIApp)
> {
> CGIApp = [[CGIApplication alloc] init];
> }
> pthread_mutex_unlock(&_CGI_ApplicationStartingMutex);
> }
> return CGIApp;
> }
>
> @end_______________________________________________
_______________________________________________
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