आपको लगता है कि संकलन नीचे कोड से देख सकते हैं और उम्मीद उत्पादन देता है सकते हैं, को बढ़ावा देने :: बाँध मुक्त काम करता है, सदस्य कार्यों और स्थिर सदस्य कार्यों के साथ बढ़ावा :: धागा प्रयोग करने के लिए पूरी तरह से अनावश्यक है:
#include <boost/thread/thread.hpp>
#include <iostream>
void FreeFunction()
{
std::cout << "hello from free function" << std::endl;
}
struct SomeClass
{
void MemberFunction()
{
std::cout << "hello from member function" << std::endl;
}
static void StaticFunction()
{
std::cout << "hello from static member function" << std::endl;
}
};
int main()
{
SomeClass someClass;
// this free function will be used internally as is
boost::thread t1(&FreeFunction);
t1.join();
// this static member function will be used internally as is
boost::thread t2(&SomeClass::StaticFunction);
t2.join();
// boost::bind will be called on this member function internally
boost::thread t3(&SomeClass::MemberFunction, someClass);
t3.join();
}
आउटपुट:
hello from free function
hello from static member function
hello from member function
निर्माता में आंतरिक बाँध आप के लिए सभी काम करता है।
बस प्रत्येक फ़ंक्शन प्रकार के साथ क्या होता है, इसके बारे में कुछ अतिरिक्त टिप्पणियां जोड़ दी गईं। (उम्मीद है कि मैंने स्रोत को सही तरीके से पढ़ा है!) जहां तक मैं देख सकता हूं, बूस्ट :: बाइंड का उपयोग करके बाहरी रूप से इसे डबल अप करने और आंतरिक रूप से कॉल करने का कारण नहीं बनता है क्योंकि यह इस तरह से गुजरता है।
तो मुझे लगता है तुम सच में खत्म हो गया (देशी) पी धागे पूछ रहे हैं धागे के लिए क्यों उपयोग बूस्ट? – ScoPi
@ScoPi: नहीं, मुझे लगता है कि वह पूछ रहा है कि 'बूस्ट :: थ्रेड' कन्स्ट्रक्टर को केवल सभी काम करने की बजाय 'बांध' का उपयोग क्यों करें। (और मुझे पूरा यकीन है कि आपको 'bind' का उपयोग करने की आवश्यकता है यदि आप 'shared_ptr' का उपयोग करके थ्रेड शुरू करना चाहते हैं।) –