I'm trying to create a vector of threads that perform a search. This is the important stuff from my SearchThread class.
class SearchThread {
explicit SearchThread(int t_id) {
id = t_id;
run = true;
thread = std::jthread([&]{
while(run){
{
std::unique_lock lock(mtx);
cnd_var.wait(lock, [&] { return !run; });
if(!run) { return; }
}
Search();
}
});
}
void Search();
int id;
bool run;
std::jthread thread;
std::condition_variable cnd_var;
std::mutex mtx;
}
And i'm attempting to construct them like this.
std::vector<SearchThread> search_threads;
for (int t_id = 0; t_id < num_threads; t_id++) {
search_threads.emplace_back(t_id);
}
The error i'm getting is no matching function for call to 'construct_at'. The issue i believe is with std::mutex and std::conditional_variable. I can have the mutex and conditional variable declared as pointers and then construct them on the heap in the class constructor, however that seems very ugly to me.
I guess my question is, why am i not allowed to do it like this, and is there a way around it?