Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

c++ - How to pass an argument to boost::thread?

thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) );  

is it possible that run has an argument like this :

void clientTCP::run(boost:function<void(std::string)> func);

and if yes how my boost::thread call should be written

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code boost::bind( &clientTCP::run , this ) defines a function callback. It calls the function run on the current instance (this). With boost::bind you can do the following:

// Pass pMyParameter through to the run() function
boost::bind(&clientTCP::run, this, pMyParameter)

See the documentation and example here:
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html

If you wish to construct an instance of boost::thread with a function or callable object that requires arguments to be supplied, this can be done by passing additional arguments to the boost::thread constructor:

void find_the_question(int the_answer);

boost::thread deep_thought_2(find_the_question,42);

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...