From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,3ea3751944fb5801 X-Google-Attributes: gidfee84,public From: denham@denham.zk3.dec.com (Jeffrey M. Denham) Subject: Re: Threads Date: 1995/04/11 Message-ID: <1995Apr11.170345.14566@decvax.dec.com>#1/1 X-Deja-AN: 100181222 sender: usenet@decvax.dec.com (Usenet News System) references: <3lpjq8$r07@bcarh8ab.bnr.ca> organization: Digital Equipment Corporation reply-to: denham@zk3.dec.com (Jeffrey M. Denham) newsgroups: comp.unix.osf.osf1 In article <3lpjq8$r07@bcarh8ab.bnr.ca>, danr@bnr.ca (Dan Reynolds) writes: |>Since we are blessed by so many DEC employees in our newsgroup, I have a |>question regarding DEC threads and the use of thread safe code. |> |>To obtain thread safe code one must remove or protect references to the |>following dependencies: |> |> o Static (or global) read-write data |> o File descriptors shared between threads |> o REFERENCES TO GLOBAL ERRNO |> o calls to other nonthread-safe routines. |> |> |>The problem is that the GLOBAL ERRNO is used by many UNIX functions, such as |>file I/O, sockets, semaphores, etc..., and even the PTHREAD library. |> |>So, my question is, how can one write thread safe code, when a significant |>number of UNIX functions are using the GLOBAL ERRNO, which includes the PTHREAD |>library? |> |>thanks... |> |>Dan |> |>-- |>Dan Reynolds | (613)-763-7584 | Opinions expressed are my own and do |>Bell-Northern Research | | not necessarily reflect my employer. |>Ottawa, Ontario, Canada | danr@bnr.ca | |>------------------------------------------------------------------------------- |> Here's a reply from a DECthreads developer who hasn't had time to monitor the newsgroup lately: That's simply untrue! DECthreads and libc_r do write the global "int errno" when they report an error, but that's strictly secondary and only for compatibility with code that's not built using _REENTRANT. They ALSO set a per-thread errno, and that's what all code should be reading when using thread-safe libraries. DECthreads provides _errno(), which returns the address of a per-thread errno cell. And (when the _REENTRANT or _THREADSAFE flag is defined) errno.h #defines errno to call _errno() and dereference it. The result is "virtually indistinguishable" from a global errno, but it's completely thread safe with no synchronization. Including cma.h or pthread.h automatically defines _REENTRANT, so as long as errno.h is included after one of those you get it for free. If you don't include one of those headers, or if you include errno.h first, you'll get the global errno.