Lazy property is initialized twice
up vote
0
down vote
favorite
As we all know lazy property is not thread safe.
my evaluation part is like this.
if someclass isInitialized {
return someclass instance
}
else{
initialize someclass
return someclass instance
}
The problem is
If thread 1 sees that someclass is not initialised it will then try to do so.
If thread 2 sees that someclass is not initialised before thread 1 gets to do so, then it too will try to initialise it too.
so it is initialized twice
can we overcome this with lazy property?
swift lazy-evaluation lazy-initialization
add a comment |
up vote
0
down vote
favorite
As we all know lazy property is not thread safe.
my evaluation part is like this.
if someclass isInitialized {
return someclass instance
}
else{
initialize someclass
return someclass instance
}
The problem is
If thread 1 sees that someclass is not initialised it will then try to do so.
If thread 2 sees that someclass is not initialised before thread 1 gets to do so, then it too will try to initialise it too.
so it is initialized twice
can we overcome this with lazy property?
swift lazy-evaluation lazy-initialization
Have a look at stackoverflow.com/questions/24024549/… and developer.apple.com/documentation/swift/cocoa_design_patterns/…, it shows how to use static properties to (lazily) instantiate shared instances (or other resources) in a thread-safe way.
– Martin R
Nov 21 at 7:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
As we all know lazy property is not thread safe.
my evaluation part is like this.
if someclass isInitialized {
return someclass instance
}
else{
initialize someclass
return someclass instance
}
The problem is
If thread 1 sees that someclass is not initialised it will then try to do so.
If thread 2 sees that someclass is not initialised before thread 1 gets to do so, then it too will try to initialise it too.
so it is initialized twice
can we overcome this with lazy property?
swift lazy-evaluation lazy-initialization
As we all know lazy property is not thread safe.
my evaluation part is like this.
if someclass isInitialized {
return someclass instance
}
else{
initialize someclass
return someclass instance
}
The problem is
If thread 1 sees that someclass is not initialised it will then try to do so.
If thread 2 sees that someclass is not initialised before thread 1 gets to do so, then it too will try to initialise it too.
so it is initialized twice
can we overcome this with lazy property?
swift lazy-evaluation lazy-initialization
swift lazy-evaluation lazy-initialization
asked Nov 21 at 7:35
Naveen Paulsingh
244
244
Have a look at stackoverflow.com/questions/24024549/… and developer.apple.com/documentation/swift/cocoa_design_patterns/…, it shows how to use static properties to (lazily) instantiate shared instances (or other resources) in a thread-safe way.
– Martin R
Nov 21 at 7:39
add a comment |
Have a look at stackoverflow.com/questions/24024549/… and developer.apple.com/documentation/swift/cocoa_design_patterns/…, it shows how to use static properties to (lazily) instantiate shared instances (or other resources) in a thread-safe way.
– Martin R
Nov 21 at 7:39
Have a look at stackoverflow.com/questions/24024549/… and developer.apple.com/documentation/swift/cocoa_design_patterns/…, it shows how to use static properties to (lazily) instantiate shared instances (or other resources) in a thread-safe way.
– Martin R
Nov 21 at 7:39
Have a look at stackoverflow.com/questions/24024549/… and developer.apple.com/documentation/swift/cocoa_design_patterns/…, it shows how to use static properties to (lazily) instantiate shared instances (or other resources) in a thread-safe way.
– Martin R
Nov 21 at 7:39
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407227%2flazy-property-is-initialized-twice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have a look at stackoverflow.com/questions/24024549/… and developer.apple.com/documentation/swift/cocoa_design_patterns/…, it shows how to use static properties to (lazily) instantiate shared instances (or other resources) in a thread-safe way.
– Martin R
Nov 21 at 7:39