在异步模式下使用spdlog进行日志记录时,如何为spdlog正在使用的线程设置cpu相关性



我使用spdlog以异步模式进行日志记录。我想把日志记录的任务只分配给一个cpu核心。有没有API可以在spdlog中实现这一点?

目前,我有一个解决方法,可以在库文件threadpool-inl.h 中创建线程池时设置相关性

SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start)
: q_(q_max_items)
{
//printf("number of threads %lu", threads_n);
if (threads_n == 0 || threads_n > 1000)
{
throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
"range is 1-1000)");
}
for (size_t i = 0; i < threads_n; i++)
{
threads_.emplace_back([this, on_thread_start] {
on_thread_start();
this->thread_pool::worker_loop_();
});
// Create a cpu_set_t object representing a set of CPUs. Clear it and mark only CPU 2 as set.
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(2, &cpuset);
int rc = pthread_setaffinity_np(threads_[i].native_handle(), sizeof(cpu_set_t), &cpuset);
if (rc != 0) {
printf( "Error calling pthread_setaffinity_np: %d n", rc);
}
}
}

最新更新