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

  • Home
  • SEARCH
  • 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 6610949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:54:24+00:00 2026-05-25T19:54:24+00:00

Suppose we have an unmanaged process which loads a managed DLL. Within the DLL

  • 0

Suppose we have an unmanaged process which loads a managed DLL. Within the DLL there is the need to block some or all threads of the unmanaged application to synchronize concurrent access to some memory.
How can one block those unmanaged threads? I only have the binary of the unmanaged application, so no recompile is possible.

  • 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-05-25T19:54:25+00:00Added an answer on May 25, 2026 at 7:54 pm

    I call to your attention the “Remarks” section of the docs for the SuspendThread API function:

    This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. (emphasis mine)

    That should suggest to you that what you’re trying is a bad idea. So should the fact that the managed API for suspending threads (Thread.Suspend) turned out to be so problematic that Microsoft actually removed it from the .NET Framework.

    The fact is, you cannot safely suspend a thread in the same process as you, without some cooperation from that thread. As one example, you could end up suspending another thread while it’s in the middle of allocating memory, and has a lock on the heap’s control structure. Bam: your thread will deadlock if you try to do anything that allocates memory, which includes JIT-compiling your .NET code. (And the heap lock is just one possible deadlock out of many. And there are race conditions. Just don’t do it.)

    As Hans said in the comments above, you cannot implement thread-safe code by suspending threads. You must cooperate with the other thread, by sharing a synchronization object (mutex, slim reader/writer lock, monitor, etc.) and having both sides appropriately signal that synchronization object to say “I’m starting to access this shared resource” and “I’m done”. If one side won’t cooperate, then you cannot have thread safety.


    If you really must suspend your unmanaged app to update memory locations, and you really can’t get that app’s cooperation, the safest way would probably be to forget about writing an in-process DLL, and instead look to the debugger API. Here’s an article that demos how to use some of the debugger API from .NET.

    Make your app a separate process that attaches to your unmanaged app as a debugger (either by attaching after the app is launched, or by starting the app under debug control), and can then control it from outside just as if you were running the unmanaged app under the Visual Studio debugger — suspend, modify memory, resume. Now that you’re in a separate process, you aren’t sharing the heap lock (or most of the other deadlock problems) with the app you’re trying to suspend — so the reasons to avoid SuspendThread largely go away. Since you are writing a debugger (of sorts), now calling SuspendThread is appropriate.

    But you’ll still have to deal with the race conditions yourself. Think carefully about what happens if another thread is halfway through writing to your block of memory. (When you resume that thread, it’ll write the other half, corrupting the data you just put there.) And what if another thread is halfway through reading your block of memory — if it’s read the first few bytes, made a decision based on them, and is about to read the next few bytes? If it just read that memory, has a copy of it in its registers, and is about to write it back with changes? Tread carefully.

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

Sidebar

Related Questions

Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose you have a window with multiple buttons such as Ok/Cancel or Yes/No/Cancel. All
I need an opinion on writing a managed (C#) wrapper for an unmanaged C++
Suppose you have an array of 1000 random integer numbers and you need to
say I have var hGCFile = GCHandle.Alloc(buffer, GCHandleType.Pinned) And I P/Invoke some unmanaged function
Suppose you have a socket listening on a TCP port, and some clients are
I have this problem, which I am dealing with for some time already. At
Suppose I have an EC2 instance, which I understood is a VM instance. So

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.