使用C ++ 17:有
using working_t = bool;
std::map<std::thread, working_t> _pool;
我正在尝试向此地图添加一个新线程,但我找不到合适的语法..
class ThreadPool {
std::map<std::thread, working_t> _pool;
void init() {
_pool.emplace(&ThreadPool::thread_init, this, false);
}
void thread_init();
};
这应该添加一个线程到地图,并将false作为值,但它无法编译..这可能吗?
答案 0 :(得分:3)
你需要:
_pool.emplace(
std::piecewise_construct,
std::forward_as_tuple(&ThreadPool::thread_init, this),
std::forward_as_tuple(false)
);
...区分键和值的参数列表。但是,您会遇到std::thread
没有operator <
的问题,但这会引发另一个问题:)