2013-04-18 100 views
2

這是我第一次使用boost線程函數,在此之前,我對使用多線程有一點了解。我試圖在初始調用旁邊運行函數的第二個實例,這樣我就可以將兩個不同的變量傳遞給同一個函數,我希望能夠加快程序的運行速度。與我的代碼知道,我不斷收到一個C2784錯誤,說在boost :: thread函數中傳遞參數

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

這裏的代碼,與線程創建

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(); 

編輯

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

「轉換::轉換」的外觀是什麼樣的? – juanchopanza

+0

@juanchopanza整個功能相當長,但這就是它的定義/開始,如果你想要更多,我可以添加它 – user1704863

+1

是'Conversion :: conversion'成員函數嗎? – juanchopanza

回答

3

成員函數取式(CV合格)T*,其中T與成員函數的類的一個隱式的第一個參數。您需要將指針傳遞給Conversion實例,例如,

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

非常感謝! – user1704863

0

使用boost::bind交易的片段。

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

這是假定Conversion :: conversion是一個成員函數。如果Conversion :: conversion不是成員函數,則省略conversion_obj_ptr參數。

編輯

正如其他人評論你不需要使用bind,該boost::thread構造函數會爲你做的。

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

+0

好吧,只是不知道什麼後期望* conversion_obj_ptr = – user1704863

+0

你實際上並不需要在這裏使用'bind'。 – juanchopanza

+0

@PeterR - 構造函數將參數綁定在一起。 –