Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create thread inside class with function from same class

Tags:

I want to be able to define a class with some data members, and a function which has access to those data members, which are to be private.

I then want a public function, which creates some threads, which operate on the data members of the class. I am having some trouble getting my code to compile.

Don't worry about mutex or data protection, this isn't going to be a problem, since this is just some example code for testing.

class foo {     public:     void make_foo_func_threads();      private:     void foo_func();      char private_data;     std::vector<std::thread> some_threads; }  void foo::foo_func() {     while(1) {         private_data = 'A';     } }  void foo::make_foo_func_thread() {     for(...) some_threads.push_back(std::thread(foo_func));     for(...) some_threads.join(); } 

The compiler is giving me the error:

'no matching call to std::thread::thread()'

Apparently there is 'no known conversion for argument 1 from <unresolved overloaded function type> to void (foo::*&&)'.

Erm, yeah, I have no idea what that means apart from the compiler is having trouble understanding how to resolve foo_func - I think.

How can I help the compiler understand what I am trying to do, so it won't bother me with any more errors. No doubt the code I have written is not legal, and if that is the case could someone explain why that is the case to me. Thanks!

like image 677
FreelanceConsultant Avatar asked Jul 04 '13 14:07

FreelanceConsultant


People also ask

How will you launch a thread using function pointer and function object?

To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable. After defining callable, pass it to the constructor.

Can a thread spawn another thread C++?

A thread does not operate within another thread. They are independent streams of execution within the same process and their coexistence is flat, not hierarchical. Some simple rules to follow when working with multiple threads: Creating threads is expensive, so avoid creating and destroying them rapidly.


1 Answers

foo_func is a (non-static) member function, and it needs an instance of foo on which to operate. This instance must be provided to the thread constructor. If you refer to the std::thread::thread reference page it explains what code is executed in the new thread. The relevant point is that which refers to f being a pointer to member function:

  • If f is pointer to a member function of class T, then it is called. The return value is ignored. Effectively, the following code is executed:
    • (t1.*f)(t2, ..., tN) if the type of t1 is either T, reference to T or reference to type derived from T.
    • ((*t1).*f)(t2, ..., tN) otherwise.

so it is clear that the instance is required.

Change to:

for(...) some_threads.push_back(std::thread(&foo::foo_func, this)); 

Simple example:

#include <iostream> #include <thread> #include <vector>  class foo { public:     void make_foo_func_threads()     {         for (int i = 0; i < 5; ++i)             some_threads.push_back(std::thread(&foo::foo_func, this));         for (auto& t: some_threads) t.join();     }  private:     void foo_func() { std::cout << "Hello\n"; }     std::vector<std::thread> some_threads; };  int main() {     foo f;     f.make_foo_func_threads(); } 
like image 184
hmjd Avatar answered Oct 04 '22 14:10

hmjd