#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "test";
auto f = [&str](int a, int b) {
cout << str << endl;
cout << a + b << endl;
};
f(1, 2);
return 0;
}
(2) 将lambda表达式作为thread传参:
#include <iostream>
#include <string>
#include <thread>
using namespace std;
int main() {
string str = "test";
thread func = thread([&str](int a, int b) {
cout << str << endl;
cout << a + b << endl;
}, 1, 2);
func.join();
return 0;
}
这里局部变量str传入线程,需要用捕获的方式,否则需要将其定义为全局变量才可以:
#include <iostream>
#include <string>
#include <thread>
using namespace std;
string str = "test";
int main() {
thread func = thread([](int a, int b) {
cout << str << endl;
cout << a + b << endl;
}, 1, 2);
func.join();
return 0;
}
4.多线程加速
(1) 任务切割:将一个任务切割成多个子任务;
(2) 多线程加速:每个子任务开一个线程执行,从而实现加速的目的;
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;
template <class T>
void Measure(T&& func) {
auto start = system_clock::now();
func();
duration<double> diff = system_clock::now() - start;
cout << "TimeCost: " << diff.count() << " seconds." << endl;
}
void Sum(long start, long end, long& ans) {
long s = 0;
for (long i = start; i < end; ++i) {
s += i;
}
ans = s;
}
const long S = 100000000;
int main() {
Measure([]() {
long ans1, ans2;
thread t1 = thread(Sum, 0, S / 2, ref(ans1));
thread t2 = thread(Sum, S / 2, S, ref(ans2));
t1.join();
t2.join();
cout << "Ref Answer: " << (ans1 + ans2) << endl;
});
Measure([]() {
long ans;
Sum(0, S, ans);
cout << "Normal Answer: " << ans << endl;
});
return 0;
}
#include <iostream>
#include <thread>
#include <chrono>
#include <future>
using namespace std;
using namespace std::chrono;
template <class T>
void Measure(T&& func) {
auto start = system_clock::now();
func();
duration<double> diff = system_clock::now() - start;
cout << "TimeCost: " << diff.count() << " seconds." << endl;
}
long Sum(long start, long end) {
long s = 0;
for (long i = start; i < end; ++i) {
s += i;
}
return s;
}
const long S = 100000000;
int main() {
Measure([]() {
const long K = 2;
const long Step = S / K;
vector<future<long>> vf;
vf.reserve(K);
for (int i = 0; i < K; ++i) {
vf.push_back(async(
Sum, i == 0 ? 0 : Step * i,
i == K - 1 ? S : Step * (i + 1))
);
}
long ans = 0;
for (int i = 0; i < K; ++i) {
ans += vf[i].get();
}
cout << "Async: " << ans << endl;
});
Measure([]() {
long ans = Sum(0, S);
cout << "Normal Answer: " << ans << endl;
});
return 0;
}
6.锁问题
(1) 多个线程对对同一个变量操作,会出现数据竞态问题;
(2) 加锁的目的是为了保证同一个时刻,只有一个线程能够对变量进行操作;
#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <vector>
using namespace std;
using namespace std::chrono;
mutex mtx;
template <class T>
void Measure(T&& func) {
auto start = system_clock::now();
func();
duration<double> diff = system_clock::now() - start;
cout << "TimeCost: " << diff.count() << " seconds." << endl;
}
void Sum(int& s) {
mtx.lock();
for (int i = 0; i < 1000000; ++i) {
s++;
}
mtx.unlock();
}
int main() {
Measure([]() {
vector<thread> vf;
int s = 0;
for (int i = 0; i < 4; ++i) {
vf.emplace_back(Sum, std::ref(s));
}
for (int i = 0; i < 4; ++i) {
vf[i].join();
}
cout << s << endl;
});
return 0;
}