how to run multiple threads from a vector

I need to run this "MainLoop" many times at the same time, for each "MyClassInfos". How do I use threading properly?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


  class MyFuncClass
{
public: 
  void MainLoop() { ... }


};


  struct MyClassInfos{
  string name;
  MyFuncClass* func;
};
vector<MyClassInfos> funcinfo;


int serviceCheck() {
   
    for (int i = 0; i < funcinfo.size(); i++) {

    funcinfo.at(i).func->MainLoop(); //need to make this a new Thread. (How?)

   // std::thread td(funcinfo.at(i).func->MainLoop()); //obviously not going to work

}

}
Last edited on
1
2
3
4
5
6
7
int serviceCheck() {

    for( const auto& mci : funcinfo )
        std::thread( &MyFuncClass::MainLoop, mci.func ).detach() ;

    return 0 ;
}
this doesn't work.

"no instance of constructor "std::thread::thread" matches the argument list argument types are: (void (MyFuncClass::*)(), void ())"



"a pointer to a bound function may only be used to call the function"


1
2
3
4
5

for (const auto& mci : funcinfo ) 
     std::thread( &MyFuncClass::MainLoop, mci.func->MainLoop).detach();





1
2
3
for (const auto& mci : funcinfo ) 
     // std::thread( &MyFuncClass::MainLoop, mci.func->MainLoop).detach();
     std::thread( &MyFuncClass::MainLoop, mci.func ).detach() ;

http://coliru.stacked-crooked.com/a/93fb2169da1b6aac
I'm calling MainLoop from a Class, not struct. I edited the post.
There is no difference between the keywords class and struct other than the default access (and inheritance) modes.

Using the keyword class instead of struct: http://coliru.stacked-crooked.com/a/73d1688a590e2a99
Post a complete compilable test program that shows this problem.
Topic archived. No new replies allowed.