YSTest: concurrency.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

concurrency.cpp
浏览该文件的文档.
1 /*
2 © 2014 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 "ystdex/any.h"
29 #include <sstream>
30 #if __STDCPP_THREADS__ || (defined(__GLIBCXX__) \
31 && defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)) \
32 || (defined(_LIBCPP_VERSION) && !defined(_LIBCPP_HAS_NO_THREADS))
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, std::function<void()> on_enter,
51 std::function<void()> on_exit)
52 : workers(n)
53 {
54 for(size_t i = 0; i < n; ++i)
55 workers.emplace_back([=]{
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()
130 {
131 auto& threads(static_cast<thread_pool&>(*this));
132
133 threads.~thread_pool();
134 ::new(&threads) thread_pool(max_tasks);
135 }
136 # endif
137
138 } // namespace ystdex;
139
140 #endif
141
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:99
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
platform::containers::string
basic_string< char > string
Definition: Container.h:101
ystdex::reset
bool reset(std::unique_ptr< _type > &p)
Definition: memory.hpp:584
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
ystdex
特性测试宏。
Definition: ydef.h:796
concurrency.h
并发操作。
YSLib::i
return i
Definition: YCoreUtilities.h:176
ystdex::thread_pool::thread_pool
thread_pool(size_t, std::function< void()>={}, std::function< void()>={})
初始化：使用指定的初始化和退出回调指定数量的工作线程。
any.h
动态泛型类型。
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321