Shaolin wrote:
Hi Guys
I am trying to convert my application to a multithreaded app. Please see code below:
boost::thread_group threads; FileThread *fThreads[filearr.size()];
for(int i=0; i < filearr.size(); i++) { fThreads[i] = new FileThread(filearr[i]); threads.create_thread(fThreads[i]->find()); } threads.join_all();
As you can see from the code above, I have an array of FileThread objects, which have several functions. What I want to do, is to first off invoke the find() function which will go through some algorthms (this is already done) and then the print() function which will print the results. How can I invoke the print() function with the same thread ?
Shaolin -
Boost.Thread is going to invoke a functor or function of some sort that
takes no arguments. As a result you need make the function or functor you
want called fit that signature. Often bind is used for this. You want to call
multiple statements. A simple way is to create a helper object of some sort.
For example, you could create a structure like this (untested):
struct my_helper
{
my_helper( FileThread* object ) : object_( object ){}
void operator()() { object_->find(); object_->print(); }
};
.... then in your create_thread call....
threads.create_thread( my_helper( fThreads[ i ] ) );
Another option would be to create the "my_helper" function inline using
a lambda function. You can do this with a library such as Boost.Phoenix.
The following little example illustrates Phoenix's Block Statement or
ability to have a sequence of statements. I also took the liberty to
use shared_ptr for reference counting and a vector instead of a vanilla
array.
Hopefully this is useful.
----------------------------------------------------------
#include <vector>
#include <iostream>
#include