Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whale 问题,请教了:sp_write_tmembuf_判断不是独有后,为啥再重新生成一个? #22

Open
andylau004 opened this issue Jul 6, 2020 · 2 comments

Comments

@andylau004
Copy link

andylau004 commented Jul 6, 2020

  void AppendWriteBuf(const uint8_t *buf, uint32_t len) {
    muduo::MutexLockGuard lock(*sp_mutexlock_write_buf);
    if (!(sp_write_tmembuf_.unique())) {
      uint8_t *p_buf = 0;
      uint32_t u32_len = 0;

      sp_write_tmembuf_->getBuffer(&p_buf, &u32_len);
      sp_write_tmembuf_ =
          boost::make_shared<TMemoryBuffer>(p_buf,
                                            u32_len,
                                            TMemoryBuffer::COPY);
    }

    sp_write_tmembuf_->write(buf, len);
  }

sp_write_tmembuf_.unique

此处判断sp_write_tmembuf_是否,多个shared_ptr指向,如果被shared_ptr指向,则重新 boost::make_shared ,,why?

@tiancihui
Copy link
Member

  void AppendWriteBuf(const uint8_t *buf, uint32_t len) {
    muduo::MutexLockGuard lock(*sp_mutexlock_write_buf);
    if (!(sp_write_tmembuf_.unique())) {
      uint8_t *p_buf = 0;
      uint32_t u32_len = 0;

      sp_write_tmembuf_->getBuffer(&p_buf, &u32_len);
      sp_write_tmembuf_ =
          boost::make_shared<TMemoryBuffer>(p_buf,
                                            u32_len,
                                            TMemoryBuffer::COPY);
    }

    sp_write_tmembuf_->write(buf, len);
  }

sp_write_tmembuf_.unique

此处判断sp_write_tmembuf_是否,多个shared_ptr指向,如果被shared_ptr指向,则重新 boost::make_shared ,,why?

他的目的其实很简单,就是利用copy on write的机制减少锁的临界区,扩大并发。
具体原理: buffer会被多个线程访问,意味着需要加锁,但加锁临界区变大会导致并发度不高。
在这个buffer被某个线程访问的时候,判断下这个buffer的的智能指针否被唯一的指向,不是唯一被指向的话证明有多个线程访问,就开启写时复制,反之不然。
希望能有些帮助。

@andylau004
Copy link
Author

  void AppendWriteBuf(const uint8_t *buf, uint32_t len) {
    muduo::MutexLockGuard lock(*sp_mutexlock_write_buf);
    if (!(sp_write_tmembuf_.unique())) {
      uint8_t *p_buf = 0;
      uint32_t u32_len = 0;

      sp_write_tmembuf_->getBuffer(&p_buf, &u32_len);
      sp_write_tmembuf_ =
          boost::make_shared<TMemoryBuffer>(p_buf,
                                            u32_len,
                                            TMemoryBuffer::COPY);
    }

    sp_write_tmembuf_->write(buf, len);
  }

sp_write_tmembuf_.unique
此处判断sp_write_tmembuf_是否,多个shared_ptr指向,如果被shared_ptr指向,则重新 boost::make_shared ,,why?

他的目的其实很简单,就是利用copy on write的机制减少锁的临界区,扩大并发。
具体原理: buffer会被多个线程访问,意味着需要加锁,但加锁临界区变大会导致并发度不高。
在这个buffer被某个线程访问的时候,判断下这个buffer的的智能指针否被唯一的指向,不是唯一被指向的话证明有多个线程访问,就开启写时复制,反之不然。
希望能有些帮助。

谢谢,反馈;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants