記事ディレクトリ
理解
以前は、マルチスレッドはシステムAPIしか使用できず、クロスプラットフォームの問題を解決できませんでした。一連のコードがプラットフォームに移植され、対応するマルチスレッドを変更する必要がありました。C ++ 11は、この問題を解決するために言語レベルのスレッドを使用するだけで済みます。
参照: #include <スレッド>に含まれています
単一のCPUコアの複数のスレッド:タイムスライスは1つのスレッドのコードを実行しますが、これは実際には並列計算ではありません。
複数のCPUまたは複数のコア:真の並列コンピューティングを実現できます
シンプルな体験
QTでテストした次のテストですが、なぜWebページで実行できないのかわかりません。
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
using namespace std;
void thread_1()
{
cout<<"thread_1"<<endl;
}
void thread_2(int x)
{
cout<<"x:"<<x<<endl;
cout<<"thread_2"<<endl;
}
int main()
{
thread first ( thread_1); // spawn new thread that calls thread_1() 开启线程,并调用thread_1
thread second (thread_2,100); // spawn new thread that calls thread_2(100)开启线程,并调用thread_2(100)
std::cout << "主线程\n";
// synchronize threads:
first.join(); // pauses until first finishes 这个操作完了之后才能destroyed
second.join(); // pauses until second finishes//join完了之后,才能往下执行。
std::cout << "thread_1&thread_2线程结束.\n";//必须join完成
return 0;
}
スレッドIDを取得する
(thread :: id&this_thread :: get :: get_id())
// thread::get_id / this_thread::get_id
#include <iostream> // std::cout
#include <thread> // std::thread, std::thread::id, std::this_thread::get_id
#include <chrono> // std::chrono::seconds
std::thread::id main_thread_id = std::this_thread::get_id();
void is_main_thread() {
if ( main_thread_id == std::this_thread::get_id() )
std::cout << "This is the main thread.\n";
else
std::cout << "This is not the main thread.\n";
}
int main()
{
is_main_thread();//普通函数调用
std::thread th (is_main_thread);//子线程调用
//或者可以用"="进行赋值
//std::thread th;
//th = std::thread(is_main_thread);
th.join();
}
デタッチ
参加する必要はなく、直接破壊することができます
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
}
int main()
{
std::cout << "Spawning and detaching 3 threads...\n";
std::thread (pause_thread,1).detach();
std::thread (pause_thread,2).detach();
std::thread (pause_thread,3).detach();
std::cout << "Done spawning threads.\n";
std::cout << "(the main thread will now pause for 5 seconds)\n";
// give the detached threads time to finish (but not guaranteed!):
pause_thread(5);
return 0;
}
参加可能
このパラメーターを使用して、参加できるかどうかを確認できます
// example for thread::joinable
#include <iostream> // std::cout
#include <thread> // std::thread
void mythread()
{
// do stuff...
}
int main()
{
std::thread foo;
std::thread bar(mythread);
std::cout << "Joinable after construction:\n" << std::boolalpha;
std::cout << "foo: " << foo.joinable() << '\n';
std::cout << "bar: " << bar.joinable() << '\n';
if (foo.joinable()) foo.join();
if (bar.joinable()) bar.join();
std::cout << "Joinable after joining:\n" << std::boolalpha;
std::cout << "foo: " << foo.joinable() << '\n';
std::cout << "bar: " << bar.joinable() << '\n';
return 0;
}
演算結果:
Joinable after construction:
foo: false
bar: true
Joinable after joining:
foo: false
bar: false