• IOThreadPool :每个线程中包含一个 EventLoop ,即一个 epoll 的处理器。添加任务时,添加到通知队列, epoll 循环可以收到通知,处理任务。额外还可以添加IO事件回调。
  • CPUThreadPool :和 IOThreadPool 相比,它简单一些,添加任务时,添加到阻塞队列,以信号的形式通知线程,空闲线程执行任务。
  • ConcurrentHashMap 基于hazard pointer https://zhuanlan.zhihu.com/p/104308755
  • 为啥有concurrenthashmap还要又个atomichashmap? 实现原理差不多 https://github.com/facebook/folly/blob/master/folly/concurrency/ConcurrentHashMap.h
  • iobuf 参考的tcp协议栈里的mbuf https://github.com/facebook/folly/blob/master/folly/io/IOBuf.h

    • 这个东西我看redpanda也用了。要省network 浪费, iobuf避免不了

wangle介绍

https://my.oschina.net/fileoptions/blog/881909

  • Synchronized lock guard和metux的封装。省事儿了
  folly::Synchronized<Meta> meta_;
  meta_.withRLock([&](auto& cluster_meta) {
 .......
  });
  • Singleton

一个组合使用的例子

namespace {
struct PrivateTag {};
}  // namespace
namespace my {

class Scheduler {
 private:
  std::unique_ptr<folly::CPUThreadPoolExecutor> executor_;

 public:
  static std::shared_ptr<Scheduler> GetInstance();
  Scheduler() {
  executor_ = std::make_unique<folly::CPUThreadPoolExecutor>(
      4, std::make_shared<folly::NamedThreadFactory>("worker"));}
  ~Scheduler() { executor_->stop(); }
};

static folly::Singleton<Scheduler, PrivateTag> scheduler_singleton;
std::shared_ptr<Scheduler> Scheduler::GetInstance() { return the_singleton.try_get(); }
}

比较常规没啥说的

  • ThreadLocalPRNG

底层实现是thread local singleton,所以就规避了反复创建random device的问题

folly::ThreadLocalPRNG rng;
auto rand = folly::Random::rand32(min, max, rng);
  • folly::ReadMostlySharedPtr

读优化的shared ptr。不过用了这个就不能和普通shared ptr互通了

  • ThreadCachedInt

原理可以看这个https://blog.csdn.net/cjk_cynosure/article/details/109320285

思路就是thread_local 然后聚合一下。这样就避免了原子加减

之前也见过类似的思路,https://travisdowns.github.io/blog/2020/07/06/concurrency-costs.html

原子数组+线性探测,大量使用ThreadCachedInt

没啥说的