Thread and dll question

Posted on 2004-11-22
Last Modified: 2010-04-05
I have a small program that uses threads - if i load in a dll with functions that the threads will use, will a dll be loaded in for each thread or will only one dll be loaded and the threads share access to it?  Also, if two or more threads need to access the dll, should i ensure the dll is thread safe or is each thread able to access the dll as if the thread was a different program?
Question by:gshep
    LVL 26

    Accepted Solution

    Regarding your questions....

    1.) A dll is loaded only once per process, not per thread. Additional "loads" only increment the ref count on the dll. (You can code the DLLMain entry point to test this, eg)

    library Project1;


    procedure DLLMain(Reason: Integer);

      // Handle calling based on reason code
      case Reason of
         DLL_PROCESS_DETACH   :  ; // Check count of detatch
         DLL_PROCESS_ATTACH   :  ; // Check count of attach
         DLL_THREAD_ATTACH    :  ;
         DLL_THREAD_DETACH    :  ;



      // Change the dll proc

      // Need to call it with process attach


    2.) All process threads will share the dll. If you need to do something specific (like thread local storage, etc), then the DLLmain entry point is a good place for it.

    3.) If you want the dll to be thread safe, then you will need to code it as such. Eg, through the use of crtical section/mutex/etc access to shared resources.


    Hope this helps,

    LVL 20

    Assisted Solution

    Russell is completely right, of course. Just some little extensions:

    Local variables are always thread safe. Your dll is automatically thread safe if you're not using any global resources/variables. As soon as multiple threads have (or can potentially have) access to the same variable/resource at the same time, you need to synchronize access to it - using one of the methods Russell mentioned in 3.).
    LVL 12

    Assisted Solution

    or use a threadvar - each thread gets it's own copy of the global var :)
    but I prefer a proper sync over threadvars anytime
    LVL 26

    Expert Comment

    by:Russell Libby

    Question was answered by all three participants.

    By the way...
    "thanks" for the work you do, it truly is appreciated.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now