-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-6.cpp
37 lines (29 loc) · 1.05 KB
/
12-6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <functional>
#include <boost/thread/thread.hpp>
// A typedef to make the declarations below easier to read
typedef void (*WorkerFunPtr)(const std::string&);
template<typename FunT, // The type of the function being called
typename ParamT> // 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 << '\n';
}
int main( ) {
std::string s1 = "This is the first thread!";
std::string s2 = "This is the second thread!";
boost::thread thr1(Adapter<WorkerFunPtr, std::string>(worker, s1));
boost::thread thr2(Adapter<WorkerFunPtr, std::string>(worker, s2));
thr1.join( );
thr2.join( );
}