C++|創建多線程(multithreading)的四種方法

藉助在<thread>頭文件中定義的C++線程庫,啟動新的線程將變得非常容易。/<thread>

通過函數指針創建線程

通過函數對象創建線程

通過lambda創建線程

通過成員函數創建線程

1 通過函數指針創建線程

#include <iostream>
#include <thread>
using namespace std;
void counter(int id, int numIterations)
{
\tfor (int i = 0; i < numIterations; ++i) {
\t\tcout << "Counter " << id << " has value " << i << endl;
\t}
}
int main()
{
\tthread t1(counter, 1, 6);
\tthread t2(counter, 2, 4);
\tt1.join();
\tt2.join();
\treturn 0;
}
/*
Counter 2 has value 0
Counter 2 has value 1
Counter 2 has value 2
Counter 2 has value 3
Counter 1 has value 0
Counter 1 has value 1
Counter 1 has value 2
Counter 1 has value 3
Counter 1 has value 4
Counter 1 has value 5
*
/<thread>/<iostream>

2 通過函數對象創建線程

#include <thread>
#include <iostream>
using namespace std;
class Counter
{
public:
\tCounter(int id, int numIterations)
\t\t: mId(id), mNumIterations(numIterations)
\t{
\t}
\tvoid operator()() const
\t{
\t\tfor (int i = 0; i < mNumIterations; ++i) {
\t\t\tcout << "Counter " << mId << " has value " << i << endl;
\t\t}
\t}
private:
\tint mId;
\tint mNumIterations;
};
int main()
{
\t// Using uniform initialization syntax
\tthread t1{ Counter{ 1, 20 } };
\t// Using named variable
\tCounter c(2, 12);
\tthread t2(c);
\t// Using temporary
\tthread t3(Counter(3, 10));
\t// Wait for threads to finish
\tt1.join();
\tt2.join();
\tt3.join();
\treturn 0;
}
/*
Counter 1 has value 0
Counter 1 has value 1
Counter 1 has value 2
Counter 1 has value 3
Counter 1 has value 4
Counter 1 has value 5
Counter 1 has value 6
Counter 1 has value 7
Counter 1 has value 8
Counter 1 has value 9
Counter 1 has value 10
Counter 1 has value 11
Counter 1 has value 12
Counter 1 has value 13
Counter 1 has value 14
Counter 1 has value 15
Counter 1 has value 16
Counter 1 has value 17
Counter 1 has value 18
Counter 1 has value 19
Counter 3 has value 1
Counter 3 has value 2
Counter 3 has value 3
Counter 3 has value 4
Counter 3 has value 5
Counter 3 has value 6
Counter 3 has value 7
Counter 3 has value 8
Counter 3 has value 9
Counter 2 has value 0
Counter 2 has value 1
Counter 2 has value 2
Counter 2 has value 3
Counter 2 has value 4
Counter 2 has value 5
Counter 2 has value 6
Counter 2 has value 7
Counter 2 has value 8
Counter 2 has value 9
Counter 2 has value 10
Counter 2 has value 11
*/
/<iostream>/<thread>

3 通過lambda創建線程

#include <thread>
#include <iostream>
using namespace std;
int main()
{
\tint id = 1;
\tint numIterations = 5;
\tthread t1([id, numIterations] {
\t\tfor (int i = 0; i < numIterations; ++i) {
\t\t\tcout << "Counter " << id << " has value " << i << endl;
\t\t}
\t});
\tt1.join();
\treturn 0;
}
/*
Counter 1 has value 0
Counter 1 has value 1
Counter 1 has value 2
Counter 1 has value 3
Counter 1 has value 4
*/
/<iostream>/<thread>

4 通過成員函數創建線程

#include <thread>
#include <iostream>
using namespace std;
class Request
{
public:
\tRequest(int id) : mId(id) { }
\tvoid process()
\t{
\t\tcout << "Processing request " << mId << endl;
\t}
private:
\tint mId;
};
int main()
{
\tRequest req(100);
\tthread t{ &Request::process, &req };
\tt.join();
\treturn 0;
}
//Processing request 100
/<iostream>/<thread>

-End-


分享到:


相關文章: