विषय पर बढ़ें

अंतिम अद्यतन:2026-10-12

C++ std::thread kernel implementation

std::thread का गहरा खुलासा

जब हम एक lambda लिखकर उसे thread को देते हैं, तो thread का निर्माण और task का call कब trigger होता है?

auto t = std::thread([this] {
  // do something
});

इस thread में एक lambda पास किया गया है, तो इस section में हल करने वाला प्रश्न यह है: यह lambda कब trigger होता है? construction के समय? या कहीं call करने पर?

आइए, साथ मिलकर source code के युग में प्रवेश करें।

1. _M_make_routine

इस section में analyze किया गया code क्रमशः इस प्रकार है:

1) header file

https://github.com/gcc-mirror/gcc/blob/releases/gcc-4.9/libstdc%2B%2B-v3/include/std/thread

  1. implementation file

https://github.com/gcc-mirror/gcc/blob/releases/gcc-4.9/libstdc%2B%2B-v3/src/c%2B%2B11/thread.cc

  1. posix header file

https://github.com/gcc-mirror/gcc/blob/releases/gcc-4.9/libgcc/gthr-posix.h

ऊपर का code नीचे दिए गए constructor को call करेगा:

template<typename _Callable, typename... _Args>
explicit 
thread(_Callable&& __f, _Args&&... __args)
{
  _M_start_thread(_M_make_routine(std::__bind_simple(
          std::forward<_Callable>(__f),
          std::forward<_Args>(__args)...)));
}

__f हमारे पास किया गया lambda है, यहाँ bind से lambda को parameters के साथ bind किया जाता है, फिर उसे _M_make_routine को दिया जाता है, और _Impl construct होता है।

template<typename _Callable>
      shared_ptr<_Impl<_Callable>>
      _M_make_routine(_Callable&& __f)
      {
    // Create and allocate full data structure, not base.
    return std::make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
      }

और _Impl के अंदर अभी-अभी bind का return result _M_func stored है।

template<typename _Callable>
      struct _Impl : public _Impl_base
      {
    _Callable       _M_func;

    _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
    { }

    void
    _M_run() { _M_func(); }
      };

इसके बाद _M_make_routine द्वारा बनाया गया shared_ptr<_Impl<_Callable>>, _M_start_thread को पास किया जाता है।

2. _M_start_thread

_M_start_thread, __gthread_create को call करेगा, यहाँ execute_native_thread_routine पास किया जाता है।

void
  thread::_M_start_thread(__shared_base_type __b)
  {
    if (!__gthread_active_p())
#if __EXCEPTIONS
      throw system_error(make_error_code(errc::operation_not_permitted),
             "Enable multithreading to use std::thread");
#else
      __throw_system_error(int(errc::operation_not_permitted));
#endif

    __b->_M_this_ptr = __b;
    int __e = __gthread_create(&_M_id._M_thread,
                   &execute_native_thread_routine, __b.get());
    if (__e)
    {
      __b->_M_this_ptr.reset();
      __throw_system_error(__e);
    }
  }

मुख्य बिंदु 1: execute_native_thread_routine

नीचे दिए code में कुछ जानकारियाँ दी गई हैं:

पहला: extern "C" क्यों ज़रूरी है

दूसरा: _M_run() को call किया गया है

  extern "C"
  {
    static void*
    execute_native_thread_routine(void* __p)
    {
      thread::_Impl_base* __t = static_cast<thread::_Impl_base*>(__p);
      thread::__shared_base_type __local;
      __local.swap(__t->_M_this_ptr);

      __try
    {
      __t->_M_run();
    }
      __catch(const __cxxabiv1::__forced_unwind&)
    {
      __throw_exception_again;
    }
      __catch(...)
    {
      std::terminate();
    }

      return 0;
    }
  } // extern "C"

मुख्य बिंदु 2: __gthread_create

इसने pthread_create call किया है

static inline int
__gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
          void *__args)
{
  return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
}

ठीक है, आइए ऊपर के flow को व्यवस्थित करें: जब हम std::thread construct करते हैं, तो हमारा custom callback (lambda) पास होता है जो हमारा business logic execute करता है, std::thread का constructor pthread_create का call initiate करता है, और साथ ही execute_native_thread_routine को C language style में pthread_create को देता है, pthread_create के निष्पादन पर execute_native_thread_routine चलेगा, तब उसके अंदर का _M_run() execute होगा, यही हमारे lambda function के call के संगत है।

इस section से thread के calls की समझ और गहरी हुई, क्या आपने सीखा? share करने के लिए स्वागत है।

संबंधित विषय

टिप्पणियाँ