Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku Newsgroups: comp.os.linux.development.apps Subject: Re: object file memory Date: Mon, 6 Feb 2012 23:07:27 +0000 (UTC) Organization: A noiseless patient Spider Lines: 30 Message-ID: <20120207000059.76@kylheku.com> References: Injection-Date: Mon, 6 Feb 2012 23:07:27 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="UCk3K/YilCUd19+i749f3A"; logging-data="15268"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18n7GPZdNVxxwMDIlGRXtelz7/RAkTyghU=" User-Agent: slrn/pre1.0.0-18 (Linux) Cancel-Lock: sha1:5mD2AcM+i0C3ICetz4+H0HZ0xzY= Xref: x330-a1.tempe.blueboxinc.net comp.os.linux.development.apps:414 On 2012-02-06, Bill M wrote: > I have an object file foo.o that I want to link to an executable. The > main executable creates a number of child threads that use code and > variables from foo.o. Do you have the source code for foo.o that you can tweak and recompile? > I've included a very simplistic representation at > the end. My question is, how can each child thread (worker) get it's own > private copy of global variables foo_char and foo_int? Or does that just > happen auto-magically? Why would that happen automagically? It's completely the wrong thing which would break most programs. > ** foo.c ** > char foo_char; > int foo_int; These are static variables, which are understood to be single-instance, and any correct program relies on them being that way. Automagically making these thread-local would be broken behavior. GCC has extensions for declaring statics thread local. Check the GCC documentation for the setion on "Thread-Local Storage", but the gist of it is: __thread char foo_char; There is also an explicit POSIX API for managing thread-specific values accessed by keys: pthread_key_create, pthread_setspecific, etc.