O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Binding the thread procedure and its input to packaged_task 

In this section, you will learn how you can bind the thread function and its respective arguments with packaged_task.

Let's take the code from the previous section and modify it to understand the bind feature, as follows:

#include <iostream>#include <future>#include <string>using namespace std;int add ( int firstInput, int secondInput ) {  return firstInput + secondInput;}int main ( ) {  packaged_task<int (int,int)> addTask( add );  future<int> output = addTask.get_future();  thread addThread ( move(addTask), 15, 10);  addThread.join();  cout << "The sum of 15 + 10 is " << output.get() << endl;  return 0;}

The std::bind( ) function binds the thread function and its arguments with the respective ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required