Skip to content

Commit f661cdf

Browse files
committed
done learning thread in C++ 11
1 parent 8e2f751 commit f661cdf

File tree

7 files changed

+112
-4
lines changed

7 files changed

+112
-4
lines changed

snippet/CppThreadLearning/AsyncTest/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int main()
1111
// std::launch::deferred 0x2(2) 在调用 future::get、future::wait 时同步启动
1212
// std::launch::async | std::launch::defereed 0x3(3) 同步或异步,根据操作系统而定
1313

14-
std::async(
14+
auto f = std::async(
1515
std::launch::async,
1616

1717
[](const std::string &message)

snippet/CppThreadLearning/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
add_subdirectory (AsyncTest)
12
add_subdirectory (AtomicTest)
3+
add_subdirectory (FutureTest)
24
add_subdirectory (MutexTest)
5+
add_subdirectory (PromiseTest)
6+
add_subdirectory (ThisThreadTest)
37
add_subdirectory (ThreadTest)
4-
add_subdirectory (AsyncTest)
5-
add_subdirectory (FutureTest)

snippet/CppThreadLearning/FutureTest/main2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
void countNumber()
77
{
8-
for (int i = 0; i < 1000000000; ++i)
8+
for (int i = 0; i < 1e9; ++i)
99
;
1010
}
1111

snippet/CppThreadLearning/PromiseTest/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
#include <iostream>
2+
#include <thread>
3+
#include <future>
4+
5+
#include "sum.h"
6+
7+
8+
template <class... Args>
9+
void doIt(std::promise<int> &p, Args &&...args)
10+
{
11+
// 成员函数 set_value()
12+
// 设置 promise 的值并将 future_status 设为 ready。
13+
// void 特化:只将共享状态设为 ready。
14+
p.set_value(sum(args...));
15+
}
216

317

418
int main()
519
{
20+
// std::promise 本质上是 std::future 的一个包装。std::future的值不能被改变,但可以通过 std::promise 来创建一个拥有特定值的 std::future。
21+
// std::promise 的原理就是不改变已有 std::future 的值,而是创建新的 std::future 对象。
22+
23+
std::promise<int> p;
24+
25+
std::thread t(doIt<int, int, int>, std::ref(p), 1, 10, 100);
26+
27+
t.join();
28+
29+
// 成员函数 get_future()
30+
// 构造一个 std::future 对象,其值与 std::promise 相同,status 也与 promise 相同。
31+
std::cout << p.get_future().get() << std::endl;
632

733

834
return 0;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef _SUM_H_
2+
#define _SUM_H_
3+
4+
// copy from ./snippet/CppThreadLearning/FutureTest/sum.h
5+
6+
7+
template <typename T, typename... Args>
8+
struct LSum
9+
{
10+
11+
auto operator()(const T &val, Args &&...args)
12+
-> decltype(val + LSum<Args...>()(std::forward<Args>(args)...)) { return val + LSum<Args...>()(std::forward<Args>(args)...); }
13+
};
14+
15+
template <typename T>
16+
struct LSum<T>
17+
{
18+
19+
auto operator()(const T &val)
20+
-> decltype(val) { return val; }
21+
};
22+
23+
24+
template <typename... Args>
25+
auto sum(Args &&...args)
26+
-> decltype(LSum<Args...>()(std::forward<Args>(args)...))
27+
{
28+
return LSum<Args...>()(std::forward<Args>(args)...);
29+
}
30+
31+
32+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_executable (ThisThreadTest main.cpp)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <thread>
3+
#include <atomic>
4+
5+
6+
std::atomic<bool> ready(false);
7+
8+
9+
void count()
10+
{
11+
// std::this_thread::yield():暂时放弃线程的执行,将主动权交给其他线程(放心,主动权还会回来)。
12+
// 这里刻意这么设计,在主函数中就是为了让主线程先睡眠一段时间,改变 ready 条件以后,再执行子线程,刚好也测试了这个函数的用法。
13+
while (!ready) std::this_thread::yield();
14+
15+
for (int i = 0; i <= 2000000000; i++)
16+
;
17+
18+
// std::this_thread::get_id():获得当前线程 ID。
19+
std::cout << "Thread " << std::this_thread::get_id() << " finished!" << std::endl;
20+
21+
22+
return;
23+
}
24+
25+
26+
int main()
27+
{
28+
constexpr int threadNum = 10;
29+
30+
std::thread th[threadNum];
31+
32+
for (int i = 0; i < threadNum; ++i) th[i] = std::thread(count);
33+
34+
std::cout << "Main Thread is Sleeping!" << std::endl;
35+
36+
// std::this_thread::sleep_for():睡眠一段时间。
37+
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
38+
39+
ready = true;
40+
41+
std::cout << "Child Threads Start!" << std::endl;
42+
43+
for (int i = 0; i < threadNum; ++i) th[i].join();
44+
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)