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 6850883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:10:46+00:00 2026-05-27T01:10:46+00:00

I need to write class that loads shared libraries. The dlopen() / dlerror() sequence

  • 0

I need to write class that loads shared libraries. The dlopen() / dlerror() sequence needs a lock to be thread safe.

class LibLoader {
  public:
  LibLoader(string whichLib);
  bool Load() { Wait(lock); ... dlopen() ... dlerror() ... }
  bool Unload() { Wait(lock); ... dlclose() ... dlerror() ... }
  bool IsLoaded() {...}
  // ... access to symbols...
  private:
  static Lock lock;
}
Lock Lock::lock;

The users of this class (there will be multiple at the same time) will want to make it a static member of this class to avoid loading a shared library multiple time for every object of the class:

class NeedsALib {
public:
NeedsALib() { if (!myLib.IsLoaded()) { myLib.Load(); } }
private:
static LibLoader myLib;
}
LibLoader::myLib;

The problem with this code is that it may / will crash, because it relies on the order of statics being destructed when the program terminates. If the lock is gone before myLib it will crash….

How can this be written in a safe manner that is thread safe and doesn’t rely on the order of static destruction ?

  • 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-27T01:10:46+00:00Added an answer on May 27, 2026 at 1:10 am

    Unfortunately, I think the only way to avoid this is to both use unportable one-time initialization directives, and to avoid destroying the lock at all. There are two basic problems you need to deal with:

    1. What happens if two threads race to access the lock for the first time? [ie, you cannot lazy-create the lock]
    2. What happens if the lock is destroyed too early? [ie, you cannot statically-create the lock]

    The combination of these constraints forces you to use a non-portable mechanism to create the lock.

    On pthreads, the most straightforward way to handle this is with the PTHREAD_MUTEX_INITIALIZER, which lets you statically initialize locks:

    class LibLoader{
      static pthread_mutex_t mutex;
    // ...
    };
    
    // never destroyed
    pthread_mutex_t LibLoader::mutex = PTHREAD_MUTEX_INITIALIZER;
    

    On windows, you can use synchronous one-time initialization.

    Alternately, if you can guarentee that there will only be one thread before main runs, you can use the singleton pattern without destruction, and just force the lock to be touched before main():

    class LibLoader {
      class init_helper {
        init_helper() { LibLoader::getLock(); }
      };
    
      static init_helper _ih;
      static Lock *_theLock;
    
      static Lock *getLock() {
        if (!_theLock)
          _theLock = new Lock();
        return _theLock;
      }
      // ...
    };
    
    static init_helper LibLoader::_ih;
    static Lock *LibLoader::_theLock;
    

    Note that this makes the possibly non-portable (but highly likely to be true) assumption that static objects of POD type are not destroyed until all non-POD static objects have been destroyed. I’m not aware of any platform in which this is not the case.

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

Sidebar

Related Questions

I need to write a Java Comparator class that compares Strings, however with one
I need to write a generic class where the type parameter must be something
When I write class templates, and need to fully-specialize members of those classes, Doxygen
I need to write some Prolog programs for a class. Any recommendations?
I have a program for a C class I need to write. The program
I need write an update statement that used multiple tables to determine which rows
I need to write a Delphi application that pulls entries up from various tables
I'm making a task-based program that needs to have plugins. Tasks need to have
I need to write a program used internally where different users will have different
I need to write a web application using SQL Server 2005, asp.net, and ado.net.

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.