使用 boost::asio::thread_pool 的 C++ 线程池,为什么我不能重用我的线程?
C++ thread pool using boost::asio::thread_pool, why can't I reuse my threads?
我正在尝试 boost::asio::thread_pool
在我的应用程序中创建线程池。我创建了以下玩具示例,看看我是否理解它是如何工作的,但显然不理解:)
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/bind.hpp>
#include <iostream>
boost::asio::thread_pool g_pool(10);
void f(int i) {
std::cout << i << "\n";
}
int main() {
for (size_t i = 0; i != 50; ++i) {
boost::asio::post(g_pool, boost::bind(f, 10 * i));
g_pool.join();
}
}
程序输出
0
我对两件事感到困惑:第一,如果我正在等待线程完成使用 g_pool.join()
,为什么我不能在下一次迭代中重用线程。也就是说,我希望在后续迭代中也能看到数字 10,20,30,...
打印等
其次,我正在创建一个大小为 10 的线程池,为什么我至少没有看到 10 个输出?我无法解决这个问题。
请让我知道哪里出错了,在此先感谢!
您在发布第一个任务后加入池。所以,在你接受第二个任务之前,池就停止了。这就解释了为什么你没有看到更多。
这修复了:
for (size_t i = 0; i != 50; ++i) {
post(g_pool, boost::bind(f, 10 * i));
}
g_pool.join();
附录 #1
回应评论。如果您想等待特定任务的结果,请考虑未来:
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <future>
boost::asio::thread_pool g_pool(10);
int f(int i) {
std::cout << '(' + std::to_string(i) + ')';
return i * i;
}
int main() {
std::cout << std::unitbuf;
std::future<int> answer;
for (size_t i = 0; i != 50; ++i) {
auto task = boost::bind(f, 10 * i);
if (i == 42) {
answer = post(g_pool, std::packaged_task<int()>(task));
} else
{
post(g_pool, task);
}
}
answer.wait(); // optionally make sure it is ready before blocking get()
std::cout << "\n[Answer to #42: " + std::to_string(answer.get()) + "]\n";
// wait for remaining tasks
g_pool.join();
}
一种可能的输出:
(0)(50)(30)(90)(110)(100)(120)(130)(140)(150)(160)(170)(180)(190)(40)(200)(210)(220)(240)(250)(70)(260)(20)(230)(10)(290)(80)(270)(300)(340)(350)(310)(360)(370)(380)(330)(400)(410)(430)(60)(420)(470)(440)(490)(480)(320)(460)(450)(390)
[Answer to #42: 176400]
(280)
附录 #2:序列化任务
如果你想序列化特定的任务,你可以使用链。例如。根据参数模 3 的剩余部分序列化所有请求:
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <future>
boost::asio::thread_pool g_pool(10);
int f(int i) {
std::cout << '(' + std::to_string(i) + ')';
return i * i;
}
int main() {
std::cout << std::unitbuf;
std::array strands{make_strand(g_pool.get_executor()),
make_strand(g_pool.get_executor()),
make_strand(g_pool.get_executor())};
for (size_t i = 0; i != 50; ++i) {
post(strands.at(i % 3), boost::bind(f, i));
}
g_pool.join();
}
可能的输出:
(0)(3)(6)(2)(9)(1)(5)(8)(11)(4)(7)(10)(13)(16)(19)(22)(25)(28)(31)(34)(37)(40)(43)(46)(49)(12)(15)(14)(18)(21)(24)(27)(30)(33)(36)(39)(42)(45)(48)(17)(20)(23)(26)(29)(32)(35)(38)(41)(44)(47)
请注意,所有工作都在任何线程上完成,但是 链上的任务按照它们发布的顺序发生。所以,
- 0、3、6、9、12...
- 1、4、7、10、13...
- 2、5、8、11、14...
虽然
严格按顺序发生
- 4 和 7 不需要发生在同一个物理线程上
- 11 可能发生在 4 之前,因为它们不在同一条链上
更多
如果您需要更多“类似屏障”的同步,或者所谓的 fork-join 语义,请参阅 (我在其中发布了两个答案,一个是在我发现 fork-join 执行程序示例之后) .
我正在尝试 boost::asio::thread_pool
在我的应用程序中创建线程池。我创建了以下玩具示例,看看我是否理解它是如何工作的,但显然不理解:)
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/bind.hpp>
#include <iostream>
boost::asio::thread_pool g_pool(10);
void f(int i) {
std::cout << i << "\n";
}
int main() {
for (size_t i = 0; i != 50; ++i) {
boost::asio::post(g_pool, boost::bind(f, 10 * i));
g_pool.join();
}
}
程序输出
0
我对两件事感到困惑:第一,如果我正在等待线程完成使用 g_pool.join()
,为什么我不能在下一次迭代中重用线程。也就是说,我希望在后续迭代中也能看到数字 10,20,30,...
打印等
其次,我正在创建一个大小为 10 的线程池,为什么我至少没有看到 10 个输出?我无法解决这个问题。
请让我知道哪里出错了,在此先感谢!
您在发布第一个任务后加入池。所以,在你接受第二个任务之前,池就停止了。这就解释了为什么你没有看到更多。
这修复了:
for (size_t i = 0; i != 50; ++i) {
post(g_pool, boost::bind(f, 10 * i));
}
g_pool.join();
附录 #1
回应评论。如果您想等待特定任务的结果,请考虑未来:
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <future>
boost::asio::thread_pool g_pool(10);
int f(int i) {
std::cout << '(' + std::to_string(i) + ')';
return i * i;
}
int main() {
std::cout << std::unitbuf;
std::future<int> answer;
for (size_t i = 0; i != 50; ++i) {
auto task = boost::bind(f, 10 * i);
if (i == 42) {
answer = post(g_pool, std::packaged_task<int()>(task));
} else
{
post(g_pool, task);
}
}
answer.wait(); // optionally make sure it is ready before blocking get()
std::cout << "\n[Answer to #42: " + std::to_string(answer.get()) + "]\n";
// wait for remaining tasks
g_pool.join();
}
一种可能的输出:
(0)(50)(30)(90)(110)(100)(120)(130)(140)(150)(160)(170)(180)(190)(40)(200)(210)(220)(240)(250)(70)(260)(20)(230)(10)(290)(80)(270)(300)(340)(350)(310)(360)(370)(380)(330)(400)(410)(430)(60)(420)(470)(440)(490)(480)(320)(460)(450)(390)
[Answer to #42: 176400]
(280)
附录 #2:序列化任务
如果你想序列化特定的任务,你可以使用链。例如。根据参数模 3 的剩余部分序列化所有请求:
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <future>
boost::asio::thread_pool g_pool(10);
int f(int i) {
std::cout << '(' + std::to_string(i) + ')';
return i * i;
}
int main() {
std::cout << std::unitbuf;
std::array strands{make_strand(g_pool.get_executor()),
make_strand(g_pool.get_executor()),
make_strand(g_pool.get_executor())};
for (size_t i = 0; i != 50; ++i) {
post(strands.at(i % 3), boost::bind(f, i));
}
g_pool.join();
}
可能的输出:
(0)(3)(6)(2)(9)(1)(5)(8)(11)(4)(7)(10)(13)(16)(19)(22)(25)(28)(31)(34)(37)(40)(43)(46)(49)(12)(15)(14)(18)(21)(24)(27)(30)(33)(36)(39)(42)(45)(48)(17)(20)(23)(26)(29)(32)(35)(38)(41)(44)(47)
请注意,所有工作都在任何线程上完成,但是 链上的任务按照它们发布的顺序发生。所以,
- 0、3、6、9、12...
- 1、4、7、10、13...
- 2、5、8、11、14...
虽然
严格按顺序发生- 4 和 7 不需要发生在同一个物理线程上
- 11 可能发生在 4 之前,因为它们不在同一条链上
更多
如果您需要更多“类似屏障”的同步,或者所谓的 fork-join 语义,请参阅