سؤال

أنا أتساءل أفضل طريقة لبدء pthread الذي هو عضو في فئة C++؟نهجي الخاص يتبع كإجابة ...

هل كانت مفيدة؟

المحلول

عادةً ما أستخدم وظيفة عضو ثابتة في الفصل، وأستخدم مؤشرًا للفئة كمعلمة void *.يمكن لهذه الوظيفة بعد ذلك إما معالجة مؤشر الترابط أو استدعاء وظيفة عضو غير ثابتة أخرى باستخدام مرجع الفئة.يمكن لهذه الوظيفة بعد ذلك الرجوع إلى جميع أعضاء الفصل دون بناء جملة غريب.

نصائح أخرى

ويمكن القيام بذلك ببساطة عن طريق استخدام مكتبة التعزيز، مثل هذا:

#include <boost/thread.hpp>

// define class to model or control a particular kind of widget
class cWidget
{
public:
void Run();
}

// construct an instance of the widget modeller or controller
cWidget theWidget;

// start new thread by invoking method run on theWidget instance

boost::thread* pThread = new boost::thread(
    &cWidget::Run,      // pointer to member function to execute in thread
    &theWidget);        // pointer to instance of class

ملحوظات:

  • يستخدم هذا وظيفة عضو فئة عادية.ليست هناك حاجة لإضافة أعضاء ثابتين إضافيين مما يربك واجهة صفك
  • ما عليك سوى تضمين Boost/thread.hpp في الملف المصدر حيث تبدأ سلسلة المحادثات.إذا كنت قد بدأت للتو في التعزيز، فيمكن تجاهل كل ما تبقى من تلك الحزمة الكبيرة والمخيفة.

في C++ 11 يمكنك أن تفعل الشيء نفسه ولكن بدون تعزيز

// define class to model or control a particular kind of widget
class cWidget
{
public:
void Run();
}

// construct an instance of the widget modeller or controller
cWidget theWidget;

// start new thread by invoking method run on theWidget instance

std::thread * pThread = new std::thread(
    &cWidget::Run,      // pointer to member function to execute in thread
    &theWidget);        // pointer to instance of class

يجب عليك تمهيده باستخدام المعلمة void* :

class A
{
  static void* StaticThreadProc(void *arg)
  {
    return reinterpret_cast<A*>(arg)->ThreadProc();
  }

  void* ThreadProc(void)
  {
    // do stuff
  }
};

...

pthread_t theThread;
pthread_create(&theThread, NULL, &A::StaticThreadProc, this);

لقد استخدمت ثلاث من الطرق الموضحة أعلاه.عندما استخدمت الترابط لأول مرة في c++ استخدمته وظائف الأعضاء الثابتة, ، ثم وظائف الصديق وأخيرا تعزيز المكتبات.حاليًا أفضّل BOOST.على مدى السنوات العديدة الماضية، أصبحت متعصبًا تمامًا لـ BOOST.

BOOST هو C++ مثل CPAN بالنسبة إلى Perl.:)

توفر مكتبة Boost آلية نسخ ، مما يساعد على نقل معلومات الكائن إلى مؤشر الترابط الجديد.في مثال التعزيز الآخر، سيتم نسخ Boost::bind بمؤشر، والذي تم نسخه للتو أيضًا.لذلك سيتعين عليك الاهتمام بصلاحية الكائن الخاص بك لمنع المؤشر المتدلي.إذا قمت بتنفيذ عامل التشغيل () وتوفير مُنشئ نسخة بدلاً من ذلك وتمرير الكائن مباشرة، فلا داعي للقلق بشأنه.

حل أجمل بكثير يمنع الكثير من المتاعب:

#include <boost/thread.hpp>

class MyClass {
public:
        MyClass(int i);
        MyClass(const MyClass& myClass);  // Copy-Constructor
        void operator()() const;          // entry point for the new thread

        virtual void doSomething();       // Now you can use virtual functions

private:
        int i;                            // and also fields very easily
};

MyClass clazz(1);
// Passing the object directly will create a copy internally
// Now you don't have to worry about the validity of the clazz object above
// after starting the other thread
// The operator() will be executed for the new thread.
boost::thread thread(clazz);             // create the object on the stack

يقوم مثال التعزيز الآخر بإنشاء كائن مؤشر الترابط في الكومة، على الرغم من عدم وجود أي معنى للقيام بذلك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top