From owner-freebsd-hackers@FreeBSD.ORG Sun Oct 14 14:42:29 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 3B9793D2 for ; Sun, 14 Oct 2012 14:42:29 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (unknown [IPv6:2001:610:1108:5012::107]) by mx1.freebsd.org (Postfix) with ESMTP id C964C8FC12 for ; Sun, 14 Oct 2012 14:42:27 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id 5C9351203CC; Sun, 14 Oct 2012 16:42:23 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id 299192848C; Sun, 14 Oct 2012 16:42:23 +0200 (CEST) Date: Sun, 14 Oct 2012 16:42:23 +0200 From: Jilles Tjoelker To: Eitan Adler Subject: Re: -lpthread vs -pthread: does -D_REENTRANT matter? Message-ID: <20121014144222.GA14503@stack.nl> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Cc: FreeBSD Hackers X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 14 Oct 2012 14:42:29 -0000 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