user1704863
user1704863

Reputation: 404

pass arguments to function in a boost::thread

This is my first time using the boost thread function and prior to this I have little knowledge of working with multiple threads. I attempting to run a second instance of a function alongside the initial call so I can pass two different variables to the same function, which I'm hoping speeds my program up. with the code I have know I keep getting a C2784 Error which says

'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'const std::string'

here's the snippet of code that deals with the thread creation

string firstPart = recText.substr(1,(subPart1-1));
string secondPart = recText.substr(subPart1,subPart1);

boost::thread firstThread;
boost::thread secondThread;

firstThread = boost::thread(&Conversion::conversion,firstPart);
secondThread = boost::thread(&Conversion::conversion,secondPart);
firstThread.join();
secondThread.join();

edit

void Conversion::conversion(string _Part)
{
int value_Part = 1;
int valueShort = 0;
int value = checkValue;
if(value == value_Part)
{
      // do stuff
    }
}

Upvotes: 2

Views: 6899

Answers (2)

David
David

Reputation: 251

Use boost::bind.

Conversion *conversion_obj_ptr = ...
boost::thread firstThread;
firstThread = boost::thread(boost::bind(&Conversion::conversion, conversion_obj_ptr, firstPart);

This is assuming that Conversion::conversion is a member function. If Conversion::conversion is not a member function then leave out the conversion_obj_ptr parameter.

Edit

As others commented you don't need to use bind, the boost::thread constructor will do that for you.

http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#thread.thread_management.thread.multiple_argument_constructor

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227400

Member functions take an implicit first parameter of type (cv qualified) T*, where T is the class with the member function. You need to pass a pointer to a Conversion instance, for example,

Conversion c;
firstThread = boost::thread(&Conversion::conversion, &c, firstPart);

Upvotes: 4

Related Questions