Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:56:44+00:00 2026-06-08T01:56:44+00:00

Here’s a puzzle for you guys. I have a multithreaded program in which some

  • 0

Here’s a puzzle for you guys. I have a multithreaded program in which some threads work with managed resources like locks and semaphores. Some of the lock release primitives can only be performed from the same thread on which the lock acquire was done.

So here’s my puzzle: I wrap these kinds of operations: try { lock-acquire… do something } finally { lock-release }, but sometimes when my threads terminate, the finally clauses are performed by the .NET garbage collection thread and not by my thread. (The specific case actually involves the Dispose of an object allocated in a “using” statement; see below for details)

This is a little tricky to demo; I see it all the time in Isis2, and I’ve figured out that this is happening by checking the thread-ids in the acquire and finalize blocks. But I don’t have a 3-line demo for you, and I am sorry about that. I know it would make it easier to provide help.

Is there a way I can delay termination for a thread until all pending finalize blocks associated with that thread have executed?

—- Details added for Mark —-

What I’m really doing involves a fairly elaborate self-instrumenting locking package that has various locking abstractions (bounded buffers, barriers, normal locks, etc) with thread priorities and designed to self-detect deadlocks, priority inversions, very slow lock grants and other problems. My code is complex enough and threaded enough so that I needed this to debug it.

An example of my basic style of construct is this:

LockObject  myLock = new LockObject("AnIsis2Lock");

...

using(new LockAndElevate(myLock)) { stuff }

A LockObject is a self-monitoring lock… a LockAndElevate makes note that I locked it (in this case) and later tracks the dispose, when I unlock it. So I’m exploiting the fact that using should dispose of the new object when the block completes — even if it throws an exception.

What I’m seeing is that fairly often, threads terminate and yet the using dispose events haven’t actually occurred; they run later, on some other thread. This only happens when one of my threads terminates; in normal execution, the whole thing works like a charm.

So since using translates to try… finally, my question was posted in terms of finally clauses.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:56:50+00:00Added an answer on June 8, 2026 at 1:56 am

    So as of now, here’s my best answer to my own question, based mostly on experience debugging the behavior of Isis2.

    If threads don’t terminate, “using(x = new something()) { }” (which maps to “try { x= new something(); …} finally { x.dispose }”) works precisely as you would expect: the dispose occurs when the code block is exited.

    But exceptions disrupt the control flow. So if your thread throws an IsisException, or something down in “something” does so, control passes to the catch for that exception. This the case I’m dealing with, and my catch handler is higher on the stack. C#/.NET faces a choice: does it catch the IsisException first, or does it do the dispose first? In this case I’m fairly certain the system systematically does the IsisException first. As a result, the finalizer for the allocated object “x” has not yet executed but is runable and needs to be called soon.

    [NB: For those who are curious, the Using statement ends by calling Dispose, but the recommended behavior, per the documentation, is to have a finalizer ~something() { this.Dispose; } just to cover all possible code paths. Dispose might then be called more than once and you should keep a flag, locked against concurrency, and dispose your managed resources only on the first call to Dispose.]

    Now the key problem is that the finalizer apparently might not run before your thread has a chance to terminate in this case of a caught exception that terminates your thread; if not, C# will dispose of the object by calling dispose on a GC finalizer thread. As a result if, as in my code, x.Dispose() unlocks something an error can occur: a lock acquire/release must occur in the same thread on .NET. So that’s a source of potential bugs for you and for me! It seems that calling GC.WaitForFinalizers in my exception handler helps in this case. Less clear to me is whether this is a true guarantee that bad things won’t occur.

    A further serious mistake in my own code was that I had erroneously been catching ThreadAbortException in a few threads, due to an old misunderstanding about how those work. Don’t do this. I can see now that it causes serious problems for .NET. Just don’t use Thread.Abort, ever.

    So on the basis of this understanding, I’ve modified Isis2 and it works nicely now; when my threads terminate the finalizers do appear to run correctly, dispose does seem to happen before the thread exits (and hence before its id can be reused, which was causing me confusion), and all is good for the world. Those who work with threads, priorities, and self-managed locks/barriers/bounded buffers and semaphores should be cautious: there are dragons lurking here!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here,I have some Doubt with the output. Why the Output is same ? int
Here is the original code of Visual C++ which I have been trying to
Here is my work environment: Eclipse Juno as IDE with maven2 plugin on it
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have just tried to save a simple *.rtf file with some websites and
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is an example: I have the generic type called Account. I wish to
Here is the problem that I am trying to solve. I have two folders

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.