Sujet : Re: errno (was Re: C23 thoughts and opinions - why so conservative?)
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.cDate : 25. May 2024, 01:40:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v2rc19$2i5ih$5@dont-email.me>
References : 1 2 3 4 5
User-Agent : Pan/0.155 (Kherson; fc5a80b8)
On Fri, 24 May 2024 17:57:35 +0200, David Brown wrote:
Why would anyone want a variable that exists for /all/ threads in a
program, but independently per thread? The only use I can think of is
for errno (which is, IMHO, a horror unto itself) but since that is
defined by the implementation, it does not need to use _Thread_local.
errno is indeed the example that immediately comes to mind for the use of
this feature. It is supposed to have the semantics of an assignable
variable, so how else would you implement it, if not by some (possibly
implementation-specific or special-case equivalent of) the _Thread_local
mechanism?
I am in two minds over whether errno is a hack or not. On the one hand, it
makes more sense for system calls (and library ones, too) to return an
error status directly; on the other hand, sometimes maybe you want to
“accumulate” an error status after a series of calls, and errno is a
convenient way of doing this.
As for other uses of thread-local, I think most of them have to do with
optimizations, like threading itself. For example, imagine a bunch of
threads all contributing increments to a common counter: instead of
continually blocking on access to that counter, they could each have their
own thread-local counter, which periodically has its current value added
to the global counter and then zeroed.