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


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

Re: thread_local question...

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

Show all headers | View raw


On Sun, 2024-10-20 at 19:12 -0700, Chris M. Thomasson wrote:
> On 10/10/2024 12:20 PM, wij wrote:
> [...]
> 
> Check this shit out:
> 
> Here is my crude code:
> _______________________________________
> #include <iostream>
> #include <functional>
> #include <thread>
> #include <atomic>
> #include <mutex>
> 
> 
> #define CT_THREADS 5
> 
> 
> struct ct_shared
> {
>      std::mutex m_cout_lock;
> };
> 
> 
> struct ct_per_thread
> {
>      ct_shared& m_shared;
>      unsigned long m_id;
> 
>      ct_per_thread(
>          ct_shared& shared,
>          unsigned long id
> 
>      ):  m_shared(shared),
>          m_id(id)
>      {
>          {
>              std::unique_lock<std::mutex> lock(m_shared.m_cout_lock);
>              std::cout << "ct_per_thread::ct_per_thread(" << m_id << ")" 
> << std::endl;
>          }
>      }
> 
>      ~ct_per_thread()
>      {
>          {
>              std::unique_lock<std::mutex> lock(m_shared.m_cout_lock);
>              std::cout << "ct_per_thread::~ct_per_thread(" << m_id << 
> ")" << std::endl;
>          }
>      }
> };
> 
> 
> 
> 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
> ) {
>      {
>          thread_local ct_per_thread self(shared, id);
> 
>          g_per_thread = &self;
>      }
> 
>      ct_foo();
> }
> 
> 
> int
> main()
> {
>      std::cout << "ct_plot_pre_alpha... Testing 123! :^)\n\n";
>      std::cout << "_____________________________________________" << 
> std::endl;
> 
>      {
>          ct_shared shared;
> 
>          {
>              std::thread threads[CT_THREADS];
> 
>              std::cout << "launching " << CT_THREADS << " threads..." << 
> std::endl;
> 
>              for (unsigned long i = 0; i < CT_THREADS; ++i)
>              {
>                  threads[i] = std::thread(ct_thread, std::ref(shared), i);
>              }
> 
>              for (unsigned long i = 0; i < CT_THREADS; ++i)
>              {
>                  threads[i].join();
>              }
>          }
>      }
> 
>      std::cout << "_____________________________________________\n";
>      std::cout << "complete!\n";
> 
>      return 0;
> }
> _______________________________________

Don't know what the program is demonstrating.
The following is what I thought what ct_thread is doing.

1. In libwy, each thread is represented by a PThread object, following the RAII
   principle. (your program looked to me std::thread API has no idea of RAII).
2. libwy only has Mutex (wrapper of pthread_mutex), because other kinds of 
   mutexes are considered not often used and easily implemented (except futex)
   and many application variations, i.e. read/write mutex.
3. Wy::PThread has no public facilities for 'thread specific' (thread local), 
   because in most cases, 'thread local' can be implemented as class data 
   member. 
   (Wy::PThread is old from the time the underlying Linux Thread is buggy,
    and I am tired to revise it without real cases showing the necessarity.)

// t.cpp
#include <Wy.pthread.h>
#include <Wy.stdio.h>
    
using namespace Wy;

class Thread : public PThread {
    Mutex *m_gmtx;  // for 'thread common' data
    int   m_id;

    // Many 'thread local' data can be put here.
  public:
    Thread(Mutex& mtx, int id) : m_gmtx(&mtx), m_id(id) {};
    Errno begin() override { return PThread::begin(); };
    ~Thread() {
       NoCancel noc;
       tmain_close(); // Stop possibly active thread, then destruction begins.
       cout << "~Thread(mtx," << m_id << ")" WY_ENDL;
    };

  protected:
    Errno tmain() override {
      Mutex::Lock aa(*m_gmtx);
      cout << "Thread(mtx," << m_id << ")" WY_ENDL;
      return Ok;
    };
};

#define CT_THREADS 5

int main()
try {        
  Errno r;
  Mutex shared_mutex;
  Thread thrd[CT_THREADS]={
                   Thread(shared_mutex,0),
                   Thread(shared_mutex,1), 
                   Thread(shared_mutex,2),
                   Thread(shared_mutex,3),
                   Thread(shared_mutex,4),
                 };

 for(int i=0; i<CT_THREADS; ++i) {
   if((r=thrd[i].begin())!=Ok) {
     WY_THROW(r);
   }
 }

 for(int i=0; i<CT_THREADS; ++i) {
   if((r=thrd[i].wait_if(PThread::Running))!=Ok) {
     WY_THROW(r);
   }
 }

 return 0;
}
catch(const Errno& e) {
  cerr << wrd(e) << WY_ENDL;
  throw;
}
catch(...) {
  cerr << "Unknown thrown object" WY_ENDL;
  throw;
};

-----------------------------
[]$ g++ t.cpp -lwy
[]$ ./a.out 
Thread(mtx,0)
Thread(mtx,1)
Thread(mtx,4)
Thread(mtx,2)
Thread(mtx,3)
~Thread(mtx,4)
~Thread(mtx,3)
~Thread(mtx,2)
~Thread(mtx,1)
~Thread(mtx,0)



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