user492238
user492238

Reputation: 4084

.NET GC specification and multiple threads

Does in a finalizer method my cleanupcode need to be thread safe? Or is the GC itself ALWAYS executing in a single thread?

@Edit: clearing: it doesn't matter which thread the finalizer is executed in. I need to be sure it will only be one single thread, i.e. the finalizer will only be called exactly one time?

Upvotes: 1

Views: 195

Answers (1)

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

The standard does not specify anything about which thread finalizers are run on. From §10.9 (Automatic memory management):

"For instance, C# does not require that finalizers be run or that objects be collected as soon as they are eligible, or that finalizers be run in any particular order, or on any particular thread."

Upvotes: 2

Related Questions