Muduo library analysis - base articles (4) Thread

The thread management object, which itself carries the process tid, the function that needs to be executed

head File:

  typedef boost::function<void ()> ThreadFunc; //No parameter and no return thread function

  explicit Thread(const ThreadFunc&, const string& name = string()); //Alias ​​to prevent copy function

  CountDownLatch latch_; //Used for multi-threading to work at the same time after the conditions are met

  static AtomicInt32 numCreated_; //Atomic counter, record the number of threads


Thread private resources are defined in the CurrentThread namespace to store thread-related ids, log data, names, etc.


cpp file:

namespace CurrentThread
{
  __thread int t_cachedTid = 0; //Thread private resource, tid
  __thread char t_tidString[32];	
  __thread int t_tidStringLength = 6;	
  __thread const char* t_threadName = "unknown";
  const bool sameType = boost::is_same<int, pid_t>::value;	//判断是否int==pid_t
  BOOST_STATIC_ASSERT(sameType);
}
pid_t gettid()
{
  return static_cast<pid_t>(::syscall(SYS_gettid)); //Get the current pid (system call)
}

void afterFork()
{
	//Callback function after calling Fork
  muduo::CurrentThread::t_cachedTid = 0; //The current tid is set to 0, and the thread that calls Fork becomes the main thread
  //Other threads will not copy, the state of the lock will be copied
  muduo::CurrentThread::t_threadName = "main";
	//Modify the current tid of the thread private resource
  CurrentThread::tid();
  // no need to call pthread_atfork(NULL, NULL, &afterFork);
}

class ThreadNameInitializer
{
 public:
  ThreadNameInitializer()
  {
	  //Call once by default, and set the fork function at the same time (generally multi-threading does not fork)
    muduo::CurrentThread::t_threadName = "main";
    CurrentThread::tid();
    pthread_atfork(NULL, NULL, &afterFork);
  }
};
ThreadNameInitializer init;//Call

struct ThreadData
{
	//Each thread object calls a ThreadData, which is mainly used for decoupling, convenient method and data binding after transmission
  typedef muduo::Thread::ThreadFunc ThreadFunc;
  ThreadFunc func_;
  string name_;
  pid_t* tid_; //pointer to tid
  CountDownLatch* latch_;

  ThreadData(const ThreadFunc& func,
             const string& name,
             pid_t* tid,
             CountDownLatch* latch)
    : func_ (func),
      name_(name),
      time_ (time),
      latch_(latch)
  { }
void runInThread()
	  //Modify after pointing to tid, then clear the pointer, the memory has been modified, and the thread object still holds the pointer
void CurrentThread::cacheTid() //Store a tid once
bool CurrentThread::isMainThread()//Determine whether the thread belongs to the current thread
void CurrentThread::sleepUsec(int64_t usec) //Thread sleep
{
  struct timespec ts = { 0, 0 }; //Structure required for system call
  ts.tv_sec = static_cast<time_t>(usec / Timestamp::kMicroSecondsPerSecond);
  ts.tv_nsec = static_cast<long>(usec % Timestamp::kMicroSecondsPerSecond * 1000);
  ::nanosleep(&ts, NULL);
}
Thread::~Thread()
{
  if (started_ && !joined_) //Determine if it is running and not using join
  {
    pthread_detach(pthreadId_);//The thread is automatically destroyed after running (non-blocking)
  }
}
int num = numCreated_.incrementAndGet(); //The return value after adding one atom
void Thread::start()
{
  assert(!started_);
  started_ = true; //Change state, running
  // FIXME: move(func_)
  detail::ThreadData* data = new detail::ThreadData(func_, name_, &tid_, &latch_);
  if (pthread_create(&pthreadId_, NULL, &detail::startThread, data)) //Create a thread and bind ThreadData
  {
    started_ = false;
    delete data; // or no delete?
    LOG_SYSFATAL << "Failed in pthread_create";
  }
  else
  {
    latch_.wait();
    assert(tid_ > 0);
  }
}


Calling the fork function from multiple threads may cause deadlock, because the state of the lock will be copied, but the thread will only copy the thread calling fork, and other threads will not copy



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325801422&siteId=291194637