Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide example for using class functions #1

Closed
Keeo opened this issue Jan 28, 2015 · 3 comments
Closed

Provide example for using class functions #1

Keeo opened this issue Jan 28, 2015 · 3 comments

Comments

@Keeo
Copy link

Keeo commented Jan 28, 2015

Hi,
can you provide example how to use threadpool to call member function?

@Keeo
Copy link
Author

Keeo commented Jan 29, 2015

It can be done combination of these two exapmles.

p.push(third, "additional_param"); 
p.push( [] (int id){ std::cout << "hello from " << id << '\n'; });

//Like this:
struct Hello
{
    void print(int id) {std::cout<<"Hello from:"<<id<<std::endl;}
}

Hello hello;
p.push([](int id, Hello* hello){ hello->print(id);}, &hello); 

@Keeo Keeo closed this as completed Jan 29, 2015
@vit-vit
Copy link
Owner

vit-vit commented Jan 30, 2015

you can pass the pointer to the class in the labda capture clause.

struct Hello {
    void print(int id);
};

Hello hello;
p.push([&hello](int id){ hello.print(id); });

then a different overload of the threadpool push function is used. This overload is better because it does not use std::bind and hence does less copy constructions of parameters.
You can pass also pointer this, if you need to call a member function from another member function.

p.push([=this](int id){ this->member_function(); });

@Keeo
Copy link
Author

Keeo commented Jan 30, 2015

Thank you, lambda functions are still quite new for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants