質問

ったの ベスト 方pthread会員であるC++クラス?自分のアプローチとして以下のような答え...

役に立ちましたか?

解決

私は、通常、静的メンバー関数のクラスを使用へのポインタのクラスとしてvoid*パラメータとします。この機能は次のいずれかの条件が成立する実行のスレッド処理は、他の非staticのメンバー関数のクラスを参考にした。この機能はそれを参考すべてのクラスのつながりがない人でも、ぎこちない構文です。

他のヒント

このときだけを用いることにより、ブ図書館のようになります:

#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

注記:

  • これを使用して通常のクラスの会員機能です。ありません追加する必要があり、静的メンバーに混乱をごクラスのインタフェース
  • だけどブスレです。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++を使って 静的加機能, その 友達に機能 最後に ブ図書館.現在も進みました。過去数年間んなの力bigot.

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