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


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

Re: thread_local question...

From wij <wyniijj5@gmail.com>
Newsgroups comp.lang.c++
Subject Re: thread_local question...
Date 2024-10-11 03:20 +0800
Organization A noiseless patient Spider
Message-ID <9dc17426ed663b192a4e1467b8fc488e6be377bd.camel@gmail.com> (permalink)
References <ve7cj8$2vun0$1@dont-email.me> <ve8f6l$34vt2$1@dont-email.me> <ve98fe$392tv$2@dont-email.me>

Show all headers | View raw


On Thu, 2024-10-10 at 12:04 -0700, Chris M. Thomasson wrote:
> On 10/10/2024 4:52 AM, jseigh wrote:
> > On 10/9/24 22:02, Chris M. Thomasson wrote:
> > > How can I get access to self in my ct_foo() function below? Am I 
> > > misusing thread_local here? I want self to be able to be accessible 
> > > from any function that my ct_thread() function calls. It feels like I 
> > > am doing something wrong here. However, I am getting correct ctor's 
> > > and dtor's, I am not sure how to access self from ct_foo() called from 
> > > ct_thread()?
> > > 
> > 
> > std::thread::id id = std::this_thread::get_id();
> > 
> > C tss uses a thread local array I think but I don't know
> > it gets notification on individual thread exits so it
> > can run the dtor.  C++ has a cvar notify all on thread
> > exits.
> > 
> > I have a different hack which I won't get around to
> > until I need it.
> 
> I still don't know how C++ calls dtors wrt per-thread objects via 
> thread_local. My hack would be to use tss_create and be done with it.

That's why thread in libwy (RAII) is implemented as a class, 'thread' is an overload
virtual function in the thread class Wy::Thread (inspired by old day Qt thread). And,
'thread local' mostly is data member of the Wy::PThread. Not many app. really need
'thread local' (Other threads can access the 'local thread data').

> However, this "works", but the only reason the dtor is being called has 
> nothing to do with thread_local. I resorted to using thread_local for a 
> pointer to a local per_thread object on the threads stack. It's dtor 
> means the thread exited. However, this is a lot different than 
> tss_create or pthread_key_create. Iirc, those dtors get called a lot 
> deeper in thread shutdown.

Yes, RRAD is a problem of multi-threaded program.

> _______________________
> thread_local ct_per_thread* g_per_thread = nullptr;
> 
> void
> ct_foo()
> {
>      // Okay, what about this shit!
>      {
>          std::unique_lock<std::mutex> 
> lock(g_per_thread->m_shared.m_cout_lock);
>          std::cout << "ct_foo(" << g_per_thread->m_id << ")" << std::endl;
>      }
> }
> 
> 
> void
> ct_thread(
>      ct_shared& shared,
>      unsigned long id
> ) {
>      ct_per_thread self(shared, id);
> 
>      g_per_thread = &self;
> 
>      ct_foo();
> }
> _______________________
> 
> 
> I guess that is a hack.

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