I am trying to write a program that launched an arbitrary number of threads for execution.

#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: Solution

 thread **func_thread = new thread*[thrn]; for(auto i = 0; i < thrn; i += 1) { func_thread[i] = new thread(thf); } 
  • one
    And the text of the error as translated? - Nick Volynkin
  • Not implemented operator (). The essence of the error is clear to me. It is unclear how to get out of this situation. - Nex
  • one
    ok, cut off the most frequent problem - people don't read error messages) - Nick Volynkin
  • one
    @VladD Almost. I thought that creating a thread object! = Starting a thread for execution. - Nex
  • one
    @Nex: In C #, by the way, this is the case: the Thread object must also be launched using the .Start() method. - VladD

1 answer 1

Why not just write

  func_thread[i] = thread(thf); 

see for example here

  • Will there be a UB here? Sketched a code with a samopisny class, like all the objects created in the loop are called before exiting the loop: ideone.com/CYqXqF - Nex
  • @Nex, I do not know. However, this is not very important, because crosses (because of its internal complexity) are not the language in which to write serious things. - avp