From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,8bef52f8cd6f0ab3,start X-Google-Attributes: gidfee84,public From: Dave Butenhof Subject: Re: pthread library usage questions Date: 1996/02/15 Message-ID: <31232B8E.2F1C@zko.dec.com>#1/1 X-Deja-AN: 139663922 references: content-type: text/plain; charset=us-ascii organization: Digital Equipment Corportation mime-version: 1.0 newsgroups: comp.unix.osf.osf1 x-mailer: Mozilla 2.0 (X11; I; OSF1 X4.0 alpha) Mark LoSacco wrote: > > Hello, > > I am working on an application running under OSF/1 V3.2 which uses > the pthread library. I have some questions about the way in which applications > that use the pthread library should be compiled and linked. > > The following small sample crashes in the call to pthread_mutexattr_create(). > > #include > #include > main() { > pthread_mutexattr_t x; > > pthread_mutexattr_create(&x); > } > > When built as follows, the sample crashes: > cc test.c -lpthreads > > But when I link to the libc_r.so library, the call is successful: > cc test.c -lpthreads -lc_r > > I would assume that libc_r is the threaded version of libc. > Is there some way with flags to tell the compiler that it should use > the threaded version of libc, or is linking libc_r the correct thing > to do? > > Is anyone else out there using the pthread library successfully? > Any insights/pointers on this would be much appreciated. Please > email me directly at mark@bristol.com. > > Thanks in advance, > Mark > The proper way to build the application is: cc test.c -threads This is documented (at least) in the Guide to DECthreads, in the UNIX appendix (most of the document is platform-independent). Unfortunately, it didn't document the detailed effects of -threads, which turns out to be more important than we'd originally expected... As well as providing the proper -l list for a threaded application, the -threads switch also adds -D_THREAD_SAFE to the command line, which causes errno.h to generate a thread-safe definition for "errno" rather than using the traditional "extern int errno;". If you don't compile with this definition, your code won't work correctly in a threaded process (at least if you try to set errno for something that was built thread-safe, or read errno from something that was built thread-safe). However, if you're linking a shared library (ld doesn't support -threads), the correct -l list is: -lpthreads -lmach -lc_r -lc The problem is that, while the libpthreads.so shared library will pull in the necessary dependencies automatically when you run the program, they'll come in AFTER all the explicitly specified libraries (this supports the normal rules of UNIX symbol preemption on which many programs may depend). Thus, an implictly included libmach.so and libc_r.so will come after the libc.so library -- and both of them depend on preempting libc.so symbols. In Digital UNIX 4.0, there's a new switch, "-pthread", which builds a program using the POSIX 1003.1c-1995 interfaces. The "legacy" interfaces (cma and the POSIX draft 4 variants) are built entirely on top of the new POSIX 1003.1c-1995 "core" thread library, libpthread.so. (I know it's confusing to have "libpthread.so" and "libpthreads.so" -- but I blame OSF for choosing the old name for us. :-) ) Building with -pthread omits the legacy library so new code doesn't need to carry around the old baggage. For shared libraries, use: -lpthread -lmach -lexc -lc To link "legacy" code on Digital UNIX 4.0, use the old -threads, or: -lpthreads -lpthread -lmach -lexc -lc (And always remember the -D_THREAD_SAFE [or -D_REENTRANT] if you compile without -threads or -pthreads!) Old binaries will work on Digital UNIX 4.0, by the way, since with a thread-safe libc we no longer need to depend upon preempting libc symbols; the fact that the new libraries (including the "core" libpthread.so) will be loaded after libc is of no consequence. (The libc_r.so "library" still exists for old binaries... but is just a link to libc.so). ------------------------------------------------------------------------ Dave Butenhof Digital Equipment Corporation butenhof@zko.dec.com 110 Spit Brook Rd, ZKO2-3/Q18 Phone: 603.881.2218, FAX: 603.881.0120 Nashua, NH 03062-2711 "Better Living Through Concurrency" ------------------------------------------------------------------------