-
-
Notifications
You must be signed in to change notification settings - Fork 851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make TestFile Image threadsafe. #2225
Conversation
tests/ImageSharp.Tests/TestFile.cs
Outdated
Image<Rgba32> img = this.image; | ||
if (img is null) | ||
{ | ||
Image<Rgba32> loadedImg = ImageSharp.Image.Load<Rgba32>(this.Bytes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we can do a double check lock instead so image would never be loaded twice:
// this.image should be marked volatile still
if (this.image is null)
{
lock (this.lockObject)
{
if (this.image is null)
{
this.image = ImageSharp.Image.Load<Rgba32>(this.Bytes);
}
}
}
return this.image;
I proposed Interlocked
solution, I know, but lock
solution is a lot more readable IMO and doesn't have a possible flaw of loading the same image twice. It's also isn't slow on reads due to locking as most of the time first if
check would be true.
Maybe we should use it instead due to simplicity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely far more readable. I’m happy to switch out
tests/ImageSharp.Tests/TestFile.cs
Outdated
/// <summary> | ||
/// Used to ensure image loading is threadsafe. | ||
/// </summary> | ||
private readonly object syncLock; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private readonly object syncLock; | |
private readonly object syncLock = new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha! Can't believe I missed that!
Prerequisites
Description
Fix #2178
CC @br3aker