Пытаюсь написать программу, которая запускала был произвольное количество потоков на выполнение.
#include <iostream>
#include <thread>
using namespace std;
void thf() {
static int n = 0;
n += 1;
cout << "threads " << n << endl;
n -= 1;
return;
}
int main() {
const int thrn = 10;
thread *func_thread = new thread[thrn];
for(auto i = 0; i < thrn; i += 1) {
func_thread[i].operator()(thf); //Здесь ошибка no member named 'operator()' in 'std::__1::thread'
}
for(auto i = 0; i < thrn; i += 1) {
auto &x = func_thread[i];
if(x.joinable()) {
x.join();
}
}
return 0;
}
UPD: Вариант решения
thread **func_thread = new thread*[thrn];
for(auto i = 0; i < thrn; i += 1) {
func_thread[i] = new thread(thf);
}
Thread
нужно ещё запустить методом.Start()
. – VladD 22 сен '15 в 9:08