S
S
simply_user2015-02-22 04:06:34
Programming
simply_user, 2015-02-22 04:06:34

How to pass a class member function to the constructor of the thread class?

Hello!
I can't pass the instance method to the thread constructor in any way. I already tried to pass a pointer to the method, but I still get the same error:
error C2064: the result of fragment calculation is not a function that takes 0 arguments
I pass it like this: Thanks in advance!
thread th(&Producer::WorkingMethod);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Huseynov, 2015-02-22
@simply_user

The thread class only understands functors. A class method also needs a pointer to the class itself. Therefore, the pointer to the method, together with this, must be wrapped in a functor. Now this is most beautifully done using the labmda function. You can also use std::bind, made specifically for such cases, but IMHO the lambda is better readable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question