#include
#include
using namespace std;
void HelloWorld() {
cout << "Hello, World!" << endl;
}
int main() {
thread th(HelloWorld);
th.join();
return 0;
}
#include
#include
using namespace std;
// 1.传入一个函数指针
void Func(int x) {
for (int i = 0; i < x; ++i) {
cout << "线程使用函数指针传参." << endl;
}
}
// 2.传入一个函数对象
class FuncObj {
public:
void operator()(int x) {
for (int i = 0; i < x; ++i) {
cout << "线程使用函数对象传参." << endl;
}
}
};
int main() {
thread th1(Func, 3);
thread th2(FuncObj(), 3);
// 3.传入一个lambda函数
auto lambdaF = [](int x) {
for (int i = 0; i < x; ++i) {
cout << "线程使用Lambda函数传参." << endl;
}
};
thread th3(lambdaF, 3);
th1.join();
th2.join();
th3.join();
return 0;
}
#include
#include
using namespace std;
void func(int& result) {
std::cout << "Func thread ID: " << this_thread::get_id() << endl;
int res = 0;
for (int i = 0; i < 100; ++i) {
res += i;
}
result = res;
}
int main() {
int result = 0;
thread th(func, std::ref(result));
th.join();
std::cout << "Main thread ID: " << this_thread::get_id() << endl;
cout << result << endl;
return 0;
}
#include
#include
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;
}
#include
#include
#include
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;
}
#include
#include
#include
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;
}
#include
#include
#include
using namespace std;
using namespace std::chrono;
template
void Measure(T&& func) {
auto start = system_clock::now();
func();
duration 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
#include
#include
#include
using namespace std;
using namespace std::chrono;
template
void Measure(T&& func) {
auto start = system_clock::now();
func();
duration 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> 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;
}
#include
#include
#include
#include
#include
using namespace std;
using namespace std::chrono;
mutex mtx;
template
void Measure(T&& func) {
auto start = system_clock::now();
func();
duration 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 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;
}
C++多线程快速入门