.NODE

Passing an Argument to a Thread Function

Problem

You have to pass an argument to your thread function, but the thread creation facilities in the Boost Threads library only accept functors that take no arguments.

Solution

Create a functor adapter that takes your parameters and returns a functor that takes no parameters. You can use the functor adapter where you would have otherwise put the thread functor. Take a look at Example 12-6 to see how this is done.

Example 12-6. Passing an argument to a thread function

#include 
#include 
#include 
#include 

// A typedef to make the declarations below easier to read
typedef void (*WorkerFunPtr)(const std::string&);

template // The type of its parameter
struct Adapter {
 Adapter(FunT f, ParamT& p) : // Construct this adapter and set the
 f_(f), p_(&p) {} // members to the function and its arg

 void operator( )( ) { // This just calls the function with its arg
 f_(*p_); 
 }
private:
 FunT f_;
 ParamT* p_; // Use the parameter's address to avoid extra copying
};

void worker(const std::string& s) {
 std::cout << s << '
';
}

int main( ) {

 std::string s1 = "This is the first thread!";
 std::string s2 = "This is the second thread!";

 boost::thread thr1(Adapter(worker, s1));
 boost::thread thr2(Adapter(worker, s2));

 thr1.join( );
 thr2.join( );
}

 

Discussion

The fundamental problem you need to solve here is not specific to threading or Boost, but a general problem when you have to pass a functor with one signature to something that requires a different signature. The solution is to create an adapter.

The syntax can get a little messy, but essentially what Example 12-6 does is create a temporary functor that the thread constructor can call as a function with no arguments like it expects. First things first; use a typedef to make function pointer syntax easier to read:

typedef void (*WorkerFunPtr)(const std::string&);

This creates a type WorkerFunPtr that is a pointer to a function that takes a string reference as an argument and returns void. After that, I created the Adapter class template. It provides a way to instantiate a dynamic functor. Take a look at the constructor:

template
struct Adapter {
 Adapter(FunT f, ParamT& p) :
 f_(f), p_(&p) {}
// ...

All the constructor does is initialize the two members, which can be any types, but we expect them to be a function pointer and some parameter p of any type. I store the address of the parameter instead of copying it by value to be efficient.

Now consider this line from the main thread:

boost::thread thr1(Adapter(worker, s1));

The argument to tHR1's constructor is an instantiation of the Adapter class template, using the two types WorkerFunPtr and std::string as its arguments. That instance uses those two types for Adapter's f_ and p_ members. Finally, Adapter overrides operator( ), so it can be called like a function. When it is called, it simply does this:

f_(*p_);

Using the Adapter class template, you can pass arguments to thread functions, albeit with a little extra syntax. If you want to pass more than one argument, just add another type and member variable to Adapter. The nice thing about this approach is that you can create a set of generic adapter class templates and use them in various other contexts.

Building C++ Applications

Code Organization

Numbers

Strings and Text

Dates and Times

Managing Data with Containers

Algorithms

Classes

Exceptions and Safety

Streams and Files

Science and Mathematics

Multithreading

Internationalization

XML

Miscellaneous

Index

show all menu





C++ Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net