有没有一种方法可以检查堆叠的协同程序是否在给定链的上下文中
Is there a way to check whether a stackful coroutine is in the context of a given strand?
给定一个yield_context
对象y
和一个链s
,是否有方法检查y
是否代表s
上下文中的协程?
给定yield_context
和strand
对象,则无法检测yield_context
的执行器上下文是否为特定的strand
。然而,在协程的执行中,可以通过调用strand::running_in_this_thread()
来检测当前协程的executor上下文是否是特定的strand
。这是一个微妙的区别,但这里有一个例子来演示它的用法:
#include <cassert>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
int main()
{
boost::asio::io_service io_service;
boost::asio::io_service::strand strand1(io_service);
boost::asio::io_service::strand strand2(io_service);
boost::asio::io_service::strand strand3(strand1);
boost::asio::spawn(strand1,
[&](boost::asio::yield_context yield)
{
assert(strand1.running_in_this_thread());
assert(!strand2.running_in_this_thread());
assert(strand3.running_in_this_thread());
// Access implementation details.
auto strand4 = yield.handler_.dispatcher_;
assert(strand4.running_in_this_thread());
// No way to compare strands to one another. Although strand1, strand3,
// and strand4 use the same strand implementation, the strand objects
// are neither identifiable nor comparable.
});
io_service.run();
}
无法检测yield_context
对象的执行器上下文是否为特定strand
对象的原因是因为strand
API既不提供执行识别也不提供比较的方法。在上面的例子中,尽管strand1
、strand3
和strand4
指代相同的链实现,但推断它们使用相同实现的唯一方法是在其中一个链的执行上下文中检查每一个。此外,即使链具有可比性,yield_context
支持的API也不会暴露协程的执行器上下文。
相关文章:
- 码头化的C++应用程序是否向后兼容早期的内核版本
- 标准是否严格定义了该程序应该如何编译?
- 与 stdio 同步是否使程序 I/O 非交互式?
- 用于检查数字是否有数字或可被该数字的值整除的程序
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 为什么程序员同时使用 std::bad_alloc 和 std::exception.是否 std::例外 仅是不够的
- 用于检查值是否为其任何参数的帮助程序函数
- 是否总是可以将使用递归编写的程序重写为不使用递归的程序C++,性能观点是什么?
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 是否可以编译 Gtk+ 应用程序以同时在 Gtk 2 和 3 上运行
- 如何使用C++确定应用程序是否已在窗口中启动?
- 使程序检查当前日期是否=预期日期
- 是否可以从C++程序中调用 ACPI 方法?
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- 在程序变得低效之前,允许多少新的[]和删除[]分配是否有限制?
- 如何查询以确定我的 MacOS/X 应用程序是否处于应用程序午睡模式?
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- 这是否C++程序调用未定义的行为