global variables in shared libraries

From: martin f krafft (madduck_at_debian.org)
Date: 01/21/04

  • Next message: Katipo: "Re: FileSystems, Partition sizes, LARGE files??"
    Date: Wed, 21 Jan 2004 14:13:07 +0100
    To: debian users <debian-user@lists.debian.org>
    
    
    

    Hi there,

    Please excuse the OTness of this post. Since I am writing a library
    to be included in Debian, I feel that I should not be slaughtered
    for bothering you and hoping for your time and knowledge.

    I have a question about shared libraries. Even though I did search
    the web, I could not find a reliable answer. I am writing a shared
    library with the intention to make it reentrant and thread-safe, if
    these concepts even apply.

    My question is about global variables -- I know they are bad
    programming style, but when it comes to debugging or subsystem
    initialisation in C, they are sometimes really necessary.

    If I use a global variable in a shared library, is it shared among
    all instances using that library? Here, I would say no because
    a process' memory space is separate from another's.

    However, what happens when I have a threaded process us the library?

    I have come up with two ways to deal without global variables in C:

    The first is to expect the user to instantiate a struct, e.g.
    a struct global_settings and pass it along to each function of the
    API. This is cumbersome but would work, although I could not be
    assured that it's always the same instantiation -- the user could
    create two objects and pass them alternatingly, thereby messing with
    the integrity of the global settings and the library code. It is
    questionable whether I should guard against such a case, but I feel
    like it ;^>.

    The other approach seems cleaner: I declare "extern" variables in
    the interface and expect the user to instantiate them, while I use
    them freely in the code. The problem here is that the user would
    have to do so on a global level in his/her code, so wwe'd have the
    same problem again, albeit the responsibility would be shifted from
    the library to the user code. I am not sure this is preferable.

    I'd be interested to hear your opinions, and how you deal with these
    challenges.

    -- 
    Please do not CC me when replying to lists; I read them!
     
     .''`.     martin f. krafft <madduck@debian.org>
    : :'  :    proud Debian developer, admin, and user
    `. `'`
      `-  Debian - when you have better things to do than fixing a system
     
    Invalid/expired PGP subkeys? Use subkeys.pgp.net as keyserver!
    
    

    -- 
    To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
    with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
    


  • Next message: Katipo: "Re: FileSystems, Partition sizes, LARGE files??"

    Relevant Pages

    • Re: PAR (Was: Embedded languages based on early Ada)
      ... be like a normal Ada subprogram, ... Access to global variables is prohibited, ... maybe those libraries can be access via calls from protected objects ... normal sequential programming. ...
      (comp.lang.ada)
    • Re: Thread-Safety in Functions: A Random String Generator
      ... > I was told using static and global variables ... and the first thread executes srand() but, ... (and function libraries that are not thread safe). ...
      (comp.lang.c)
    • Re: Javascript Libraries
      ... Randy Webb wrote: ... >> Jim Ley wrote: ... > Then why would you need a "list of global variables"? ... libraries in the imperfect surrounding. ...
      (comp.lang.javascript)
    • Re: Javascript Libraries
      ... obligation, as the programmer, to ensure that you do not have name clashes. ... A "Global List of Global Names in Libraries" would be completely and utterly useless though. ... object methods and which have some global variables like "isSomething". ...
      (comp.lang.javascript)
    • Re: How to get imagebase after a DLL gets loaded
      ... how can you be unaware of the names of variables in DLL module that you ... Implementing a library that requires users of the library to instantiate ... predict what variable name you will use at initialization time. ... Some third-party libraries DO require that you instantiate a pre-defined ...
      (microsoft.public.win32.programmer.kernel)