普通视图

发现新文章,点击刷新页面。
昨天以前贾跃华

在当前线程周期回调函数

2023年5月21日 08:00

FunctionScheduler 可在当前线程周期回调函数

#include <folly/experimental/FunctionScheduler.h>
#include <iostream>
#include <gtest/gtest.h>
using namespace folly;
using namespace std;
TEST(repeatfunc,repeatfunc) { 
  FunctionScheduler fs;
  fs.addFunction([]() { cout << "Hello, world!" << endl; }, 1s);
  fs.start();
  this_thread::sleep_for(10s);
  fs.cancelAllFunctions();
}

只需要addFunction时指明要回调的函数和时间间隔。调用start启动,当不再需要回调时调用cancelAllFunctions. 本例中,每隔1s打印一次”Hello, world!”,共打印10次。

可以看到,比使用folly eventbase的asynctimer简单很多。

transparent,为关联容器增加查找成员

2023年5月18日 08:00

folly::transparent 模板类可以用比较器实例化,从而支持异型查找:

#include <set>
struct S {
  template<class T, class U>
  bool operator()(T& a, U& b) const { return a < b; }
};
struct Man {
  int id_;
  auto operator<=>(const Man&) const = default;
};
struct ManComp {
  bool operator()(const Man &l,const int& r)const
  {
    return l.id_ < r;
  }
  bool operator()(const int& l, const Man& r)const
  {
    return l < r.id_;
  }
  bool operator()(const Man& l, const Man& r)const
  {
    return l.id_ < r.id_;
  }
};
void f() {
  std::set<Man,ManComp> m;
  m.find(11);//将报错
}
void g()
{
std::set<Man,folly::transparent<ManComp>> m;
  m.find(11);
}

上述f()中的m.find(11)将报错,因为find的参数只能接受Man类型,而int类型并不能隐式转化为Man,而g()将成功,因为比较器为folly::transparent一个实例,m.find()的查找重载成员将增加一成员模板函数,理论上模板函数的参数只要能和Key也就是Man比较就可。故而使用folly::transparent后成功.

❌
❌