global variables in shared libraries
From: martin f krafft (madduck_at_debian.org)
Date: 01/21/04
- Previous message: Roelof Wobben: "Re: Re: Re: error message "unknown keyword in syslinux.cfg""
- Next in thread: Paul Morgan: "Re: global variables in shared libraries"
- Reply: Paul Morgan: "Re: global variables in shared libraries"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- application/pgp-signature attachment: Digital signature
- Previous message: Roelof Wobben: "Re: Re: Re: error message "unknown keyword in syslinux.cfg""
- Next in thread: Paul Morgan: "Re: global variables in shared libraries"
- Reply: Paul Morgan: "Re: global variables in shared libraries"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|