Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #120672

Re: thread_local question...

From Andrey Tarasevich <andreytarasevich@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: thread_local question...
Date 2024-10-09 19:33 -0700
Organization A noiseless patient Spider
Message-ID <ve7eek$30408$1@dont-email.me> (permalink)
References <ve7cj8$2vun0$1@dont-email.me>

Show all headers | View raw


On 10/09/24 7:02 PM, Chris M. Thomasson wrote:
> 
> void
> ct_thread(
>      ct_shared& shared,
>      unsigned long id
> ) {
>      // Well, self should be per thread...
>      thread_local ct_per_thread self(shared, id);
> 
>      ct_foo();
> }
> 

Um... Declaring a `thread_local` variable in block scope can serve only 
one purpose: to _hide_ the variable, to prevent anyone else from 
accessing it directly. `thread_local` is somewhat similar to `static` in 
single-threaded applications: the variable retains its value even when 
you exit and re-enter the scope. In all other respects the variable 
itself remain invisible to the outside scopes, which is the whole 
purpose of declaring it in block scope.

This is exactly what you did: you declared a variable as `thread_local` 
in block scope. Which means it will retain its value when you exit and 
re-enter the scope, but (as opposed to `static`) it is going to be a 
per-thread variable. Since you declared it locally, it implies that you 
_wanted_ to make it inaccessible from other scopes. Why are asking "how 
to get at self?" then?

The only way to access such variable from other scopes is to access it 
indirectly: through a reference/pointer that you create in advance 
(where the variable is visible) and then somehow hand that 
reference/pointer over to other scopes. If you don't what to fiddle with 
indirect access, then declare your variable as `thread_local` in 
namespace scope. That way you will easily be able to get at it from 
other functions.

However, the real question here is: why you are even declaring your 
variable as `thread_local`? A regular local variable inside a thread 
function is already as "thread local" as it can possibly get. The only 
thing that `thread_local` will give you is the retention of the old 
value between calls. Do you really need that?

-- 
Best regards,
Andrey

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 19:02 -0700
  Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 19:08 -0700
  Re: thread_local question... Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-10-09 19:33 -0700
    Re: thread_local question... Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-10-09 19:40 -0700
      Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 20:08 -0700
    Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 20:19 -0700
  Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-10 07:52 -0400
    Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-10 12:04 -0700
      Re: thread_local question... wij <wyniijj5@gmail.com> - 2024-10-11 03:20 +0800
        Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-20 19:12 -0700
          Re: thread_local question... wij <wyniijj5@gmail.com> - 2024-10-21 19:06 +0800
            Re: thread_local question... wij <wyniijj5@gmail.com> - 2024-10-21 19:20 +0800
      Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-10 15:57 -0400
        Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 13:36 -0700
      Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-11 09:50 -0400
        Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 12:21 -0700
          Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 22:53 -0700
            Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 22:56 -0700
        Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-11 17:05 -0400
          Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:44 -0700
            Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:46 -0700
  Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:29 -0700
    Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:43 -0700

csiph-web