Reputation: 3056
Can I create a single private static readonly HtmlParser HtmlParser = new HtmlParser();
and use it safely across several threads?
Or should I create a parser per thread or per usage to avoid concurrency issues?
Upvotes: 5
Views: 632
Reputation: 55389
I recommend that you create one HtmlParser per thread. Here’s why:
Upvotes: 0
Reputation: 5278
From the source it looks like it should be thread safe. The object itself has no mutable state and creating a new HtmlParser with the default options each time doesn't seem to have any benefit over creating it once, so you should be able to treat it as a singleton. The only shared state is its configuration but the default constructor uses the default configuration, which is a singleton anyway.
That being said, there isn't a huge amount of benefit to treating it as a singleton. It avoids an allocation and cuts down on GC a bit but as it doesn't have an expensive constructor, the difference won't be huge.
Upvotes: 0