How is “one-time” ReaderWriterLockSlim?

I basically follow the IDisposable pattern, and for most classes that are justified. But ReaderWriterLockSlim made me question the viability of using such a template. All ReaderWriterLockSlim.Dispose closes some event handlers. So how important is Dispose for such a class with so many resources? In this case, I really don't mind if the GC had to wait one more round for the unmanaged resource finalizers to finish.

The consequence for applying the IDisposable template is significant, however, each class that uses the one-time class must now implement IDisposable . In my specific case, I am implementing a shell for a HashSet . I don’t really expect you to want to get rid of such an object, because, by chance, it uses a synchronizer, which it does.

Is there any reason not to break the one-time pattern in this case? While I really want to, I would not do this in practice, because the violation of the sequence is much worse.

+5
source share
2 answers

The problem with unmanaged OS handles is that they are processed from a limited source. GC does not know about this.

The net memory consumed by the pen is not that big. Nothing more than an object in kernel memory and possibly a hash table entry somewhere.

You are right in saying that it is not enough to say: "You must always dispose of all disposable objects." This rule is too simple. For example, the Task class does not need to be deleted. If you know what you are doing, you can take an easier position regarding deletion. Keep in mind that not all team members can understand this point (now you can leave a link to this answer in the source code ...).

If you are sure that you will not miss many pens, you can do it safely. Keep in mind that under edge conditions (loading, errors, ...) you can skip more that you expected, causing production problems.

+4
source

If this field is static you do not need to get rid of it , it (true) will have the same lifetime as your application. I do not see this, let's move on.

The correct way to handle IDisposable is to get rid of it. I think we need a good reason not to do this.

Use a different lock:

I think it’s best to use Monitor or another lock that will have a bonus to simplify your code. ConcurrentDictionary and other wireframe classes seem to use this approach.

You are worried about locks, but I'm not sure if ReaderWriterLockSlim allowed, the only real solution is to hold fewer locks and hold them for less time.

Do not position:

This requires justification. Can you demonstrate the performance you need?

If you have several of these objects that are durable, well, not all disposable items are equally weighty (this is not like leaving an open Word document), you will probably be able to handle it. As stated, what is the point of managing all these milliseconds before the application closes anyway. I believe that the IDisposable destructor is designed to handle situations when the object is not placed, although you cannot be sure when or even if it is called.

If you have a long live application with a lot of short-lived customs of this class, however, you may have problems. You bake on your assumptions about using your code, just keep in mind.

+1
source

Source: https://habr.com/ru/post/1210114/


All Articles