logo

C++ でのマルチスレッド化

マルチスレッドは、CPU を最大限に活用するためにプログラムの 2 つ以上の部分を同時に実行できるようにする機能です。このようなプログラムの各部分はスレッドと呼ばれます。したがって、スレッドはプロセス内の軽量プロセスです。

JavaScript サンプルコードの例

マルチスレッドのサポートは C++11 で導入されました。 C++11 より前は、次を使用する必要がありました。 POSIX スレッドまたはライブラリ 。このライブラリは機能しましたが、標準の言語が提供する機能セットが欠如しているため、移植性に重大な問題が発生しました。 C++ 11 はそれらすべてを取り除き、私たちに与えてくれました。 std::スレッド 。スレッド クラスと関連関数は、 ヘッダファイル。



構文:

std::thread thread_object (callable);>

std::スレッド C++ の単一スレッドを表すスレッド クラスです。スレッドを開始するには、新しいスレッド オブジェクトを作成し、呼び出される実行コード (つまり、呼び出し可能なオブジェクト) をオブジェクトのコンストラクターに渡すだけです。オブジェクトが作成されると、新しいスレッドが起動され、callable で指定されたコードが実行されます。呼び出し可能オブジェクトは、次の 5 つのいずれかになります。

  • 関数ポインタ
  • ラムダ式
  • 関数オブジェクト
  • 非静的メンバー関数
  • 静的メンバー関数

呼び出し可能オブジェクトを定義したら、それをコンストラクターに渡します。



関数ポインタを使用したスレッドの起動

関数ポインターは、スレッドを初期化するために std::thread コンストラクターに渡す呼び出し可能オブジェクトにすることができます。次のコード スニペットは、その方法を示しています。

例:

C++






void> foo(param)> {> >Statements;> }> // The parameters to the function are put after the comma> std::>thread> thread_obj(foo, params);>

>

>

ラムダ式を使用したスレッドの起動

std::thread オブジェクトは、ラムダ式を呼び出し可能として使用して起動することもできます。次のコード スニペットは、これがどのように行われるかを示しています。

Java 8の機能

例:

C++




// Define a lambda expression> auto> f = [](params)> {> >Statements;> };> // Pass f and its parameters to thread> // object constructor as> std::>thread> thread_object(f, params);>

>

>

関数オブジェクトを使用したスレッドの起動

関数オブジェクトまたはファンクターは、C++ でスレッドを起動するために使用することもできます。次のコード スニペットは、その方法を示しています。

例:

C++




Java例外処理

// Define the class of function object> class> fn_object_class {> >// Overload () operator> >void> operator()(params)> >{> >Statements;> >}> }> // Create thread object> std::>thread> thread_object(fn_object_class(), params)>

>

>

注記 : 常に、呼び出し可能オブジェクトのパラメータを引数としてスレッド コンストラクターに個別に渡します。

非静的メンバー関数を使用したスレッドの起動

クラスの非静的メンバー関数を使用してスレッドを起動することもできます。次のスニペットは、その方法を示しています。

C++




// defining clasc> class> Base {> public>:> >// non-static member function> >void> foo(param) { Statements; }> }> // object of Base Class> Base b;> // first parameter is the reference to the functionn> // and second paramter is reference of the object> // at last we have arguments> std::>thread> thread_obj(&Base::foo, &b, params);>

>

>

静的メンバー関数を使用したスレッドの起動

静的メンバー関数を使用してスレッドを起動することもできます。

C++




// defining class> class> Base {> public>:> >// static member function> >static> void> foo(param) { Statements; }> }> // object of Base Class> Base b;> // first parameter is the reference to the function> // and rest are arguments> std::>thread> thread_obj(&Base::foo, params);>

>

Javaでの接続
>

スレッドが終了するのを待っています

スレッドが開始されたら、何らかのアクションを実行する前に、スレッドが終了するまで待つ必要がある場合があります。たとえば、アプリケーションの GUI を初期化するタスクをスレッドに割り当てる場合、GUI が適切にロードされたことを確認するために、スレッドが終了するまで待つ必要があります。

スレッドを待機するには、 std::thread::join() 関数。この関数は、現在のスレッドを、によって識別されるスレッドが待機するまで待機させます。 *これ 実行が終了しました。
たとえば、スレッド t1 が終了するまでメインスレッドをブロックするには、次のようにします。

C++




int> main()> {> >// Start thread t1> >std::>thread> t1(callable);> >// Wait for t1 to finish> >t1.join();> >// t1 has finished do other stuff> >Statements;> }>

キツネかオオカミ

>

>

マルチスレッド用の完全な C++ プログラム

C++ プログラムを以下に示します。 main 関数から 3 つのスレッドを起動します。各スレッドは、上で指定した呼び出し可能オブジェクトの 1 つを使用して呼び出されます。

C++




// C++ program to demonstrate> // multithreading using three> // different callables.> #include> #include> using> namespace> std;> // A dummy function> void> foo(>int> Z)> {> >for> (>int> i = 0; i cout << 'Thread using function' ' pointer as callable '; } } // A callable object class thread_obj { public: void operator()(int x) { for (int i = 0; i cout << 'Thread using function' ' object as callable '; } }; // class definition class Base { public: // non-static member function void foo() { cout << 'Thread using non-static member function ' 'as callable' << endl; } // static member function static void foo1() { cout << 'Thread using static member function as ' 'callable' << endl; } }; // Driver code int main() { cout << 'Threads 1 and 2 and 3 ' 'operating independently' << endl; // This thread is launched by using // function pointer as callable thread th1(foo, 3); // This thread is launched by using // function object as callable thread th2(thread_obj(), 3); // Define a Lambda Expression auto f = [](int x) { for (int i = 0; i cout << 'Thread using lambda' ' expression as callable '; }; // This thread is launched by using // lambda expression as callable thread th3(f, 3); // object of Base Class Base b; thread th4(&Base::foo, &b); thread th5(&Base::foo1); // Wait for the threads to finish // Wait for thread t1 to finish th1.join(); // Wait for thread t2 to finish th2.join(); // Wait for thread t3 to finish th3.join(); // Wait for thread t4 to finish th4.join(); // Wait for thread t5 to finish th5.join(); return 0; }>

>

>

出力 (マシンに依存)

Threads 1 and 2 and 3 operating independently Thread using function pointer as callable Thread using function pointer as callable Thread using function pointer as callable Thread using non-static member function as callable Thread using function object as callable Thread using function object as callable Thread using function object as callable Thread using lambda expression as callable Thread using lambda expression as callable Thread using lambda expression as callable Thread using static member function as callable>

注記: std::thread サポートを使用してプログラムをコンパイルするには、g++ -std=c++11 -pthread を使用します。