YSTest: concurrency.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

concurrency.cpp
浏览该文件的文档.
1 /*
2 © 2014-2016, 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "ydef.h"
29 #if __STDCPP_THREADS__ || (defined(__GLIBCXX__) \
30 && defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)) \
31 || (defined(_LIBCPP_VERSION) && !defined(_LIBCPP_HAS_NO_THREADS))
32 #include <sstream>
33 #include "ystdex/concurrency.h"
34
35 namespace ystdex
36 {
37
38 std::string
39 to_string(const std::thread::id& id)
40 {
41 std::ostringstream oss;
42
43 oss << id;
44 return oss.str();
45 }
46
47
48 # if !__GLIBCXX__ || (defined(_GLIBCXX_HAS_GTHREADS) \
49 && defined(_GLIBCXX_USE_C99_STDINT_TR1) && (ATOMIC_INT_LOCK_FREE > 1))
50 thread_pool::thread_pool(size_t n, function<void()> on_enter,
51 function<void()> on_exit)
52 {
53 workers.reserve(n);
54 for(size_t i = 0; i < n; ++i)
55 workers.emplace_back([this, on_enter, on_exit]{
56 if(on_enter)
57 on_enter();
58 while(true)
59 {
60 std::unique_lock<std::mutex> lck(queue_mutex);
61
62 condition.wait(lck, [this]{
63 return stopped || !tasks.empty();
64 });
65 if(tasks.empty())
66 {
67 // NOTE: Do nothing for spurious wakeup.
68 if(stopped)
69 break;
70 }
71 else
72 {
73 auto task(std::move(tasks.front()));
74
75 tasks.pop();
76 lck.unlock();
77 try
78 {
79 task();
80 }
81 catch(std::future_error&)
82 {
83 yassume(false);
84 }
85 }
86 }
87 if(on_exit)
88 on_exit();
89 });
90 }
91 thread_pool::~thread_pool() ynothrow
92 {
93 try
94 {
95 try
96 {
97 std::lock_guard<std::mutex> lck(queue_mutex);
98
99 stopped = true;
100 }
101 catch(std::system_error&)
102 {
103 yassume(false);
104 }
105 condition.notify_all();
106 for(auto& worker : workers)
107 try
108 {
109 worker.join();
110 }
111 catch(std::system_error&)
112 {}
113 }
114 catch(...)
115 {
116 yassume(false);
117 }
118 }
119
120 size_t
121 thread_pool::size() const
122 {
123 std::unique_lock<std::mutex> lck(queue_mutex);
124
125 return size_unlocked();
126 }
127
128 void
129 task_pool::reset(size_t tasks_num)
130 {
131 auto& threads(static_cast<thread_pool&>(*this));
132
133 threads.~thread_pool();
134 ::new(&threads) thread_pool(tasks_num);
135 }
136 # endif
137
138 } // namespace ystdex;
139
140 #endif
141
size
size_t size
Definition: memory_resource.cpp:295
ystdex::thread_pool::thread_pool
thread_pool(size_t, function< void()>={}, function< void()>={})
构造：使用指定的初始化和退出回调指定数量的工作线程。
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:96
concurrency.h
并发操作。
ydef.h
语言实现和系统环境相关特性及公用类型和宏的基础定义。
ystdex::reset
bool reset(std::unique_ptr< _type, _tDeleter > &p)
Definition: memory.hpp:613
YSLib::i
return i
Definition: YCoreUtilities.h:177
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
ystdex::thread_pool::workers
std::vector< std::thread > workers
Definition: concurrency.h:95
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
platform::ios::ostringstream
ostringstream_for< string > ostringstream
Definition: Container.h:320
ystdex::cpp2017::string
basic_string< char > string
Definition: string.hpp:772
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199