Date: Sun, 14 Oct 2012 16:42:23 +0200 From: Jilles Tjoelker <jilles@stack.nl> To: Eitan Adler <lists@eitanadler.com> Cc: FreeBSD Hackers <freebsd-hackers@freebsd.org> Subject: Re: -lpthread vs -pthread: does -D_REENTRANT matter? Message-ID: <20121014144222.GA14503@stack.nl> In-Reply-To: <CAF6rxg=PnQvtXydhx8%2BoRZJ2ERBoGwedXPcGi_9icYxAtPuxVw@mail.gmail.com> References: <CAF6rxg=PnQvtXydhx8%2BoRZJ2ERBoGwedXPcGi_9icYxAtPuxVw@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Oct 08, 2012 at 12:17:08PM -0400, Eitan Adler wrote:
> The only difference between -lpthread and -pthread that I could see is
> that the latter also sets -D_REENTRANT.
> However, I can't find any uses of _REENTRANT anywhere outside of a few
> utilities that seem to define it manually.
> Testing with various manually written pthread programs resulted in
> identical binaries, let alone identical results.
> Is there an actual difference between -pthread and -lpthread or is
> this just a historical artifact?
In some cases, -pthread also affects the compiler's code generation. On
some RISC architectures, compilers may try to avoid loads and stores of
less than 32 bits.
For example (untested):
struct { int n; char a, b, c, d; } *p;
p->a = p->b = p->c = 0;
The compiler might load p->d and then store the 32 bits containing a, b,
c and d at once. This causes a race condition if p->d is written
concurrently.
Because C99 does not specify threading, it allows these transformations.
In C11, they are forbidden. Passing -pthread disables them as well.
--
Jilles Tjoelker
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121014144222.GA14503>
