这是我尝试使用代码执行的操作的描述,请跳到下一部分以查看实际问题。
我想在嵌入式系统中使用协程,但我无法承受太多的动态分配。因此,我正在尝试以下操作:对于对外围设备的各种查询,我有不可复制、不可移动的可等待类型。查询外围设备时,我使用类似auto result = co_await Awaitable{params}
. 可等待的构造函数准备对外围设备的请求,注册其内部buffer
以接收回复,并ready
在承诺中注册其标志。然后协程被挂起。
稍后,buffer
将被填充,并且ready
标志将被设置为true
。此后,协程知道它可以恢复,这会导致等待对象在被销毁之前从缓冲区复制出结果。
可等待的内容是不可复制且不可移动的,以强制在任何地方进行有保证的复制省略,这样我就可以确保指向buffer
并ready
保持有效的指针,直到等待可等待的内容为止(至少这是计划......)
我在以下代码中遇到 ARM GCC 11.3 的问题:
#include <cstring>
#include <coroutine>
struct AwaitableBase {
AwaitableBase() = default;
AwaitableBase(const AwaitableBase&) = delete;
AwaitableBase(AwaitableBase&&) = delete;
AwaitableBase& operator=(const AwaitableBase&) = delete;
AwaitableBase& operator=(AwaitableBase&&) = delete;
char buffer[65];
};
struct task {
struct promise_type
{
bool* ready_ptr;
task get_return_object() { return {}; }
std::suspend_never initial_suspend() noexcept { return …
Run Code Online (Sandbox Code Playgroud) 我试图拥有一种最后带有可选参数的“调用”函数:
template <typename... T>
void foo(void func(T...), T... args, int opt = 0)
{
func(args...);
}
void bar(int, int);
int main()
{
foo(&bar, 1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
我本来希望这是可行的,因为参数包可以从第一个参数推导出来,但显然编译器有不同的想法:
<source>:11:5: error: no matching function for call to 'foo'
foo(&bar, 1, 2, 3);
^~~
<source>:2:6: note: candidate template ignored: deduced packs of different lengths for parameter 'T' (<int, int> vs. <>)
void foo(void func(T...), T... args, int opt = 0)
^
1 errors generated.
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)
为什么它会推导出长度为0的列表?args
我可以为了扣除的目的而强制忽略它吗?或者更一般地说,我怎样才能做到这一点?
我无法理解为什么我的应用程序需要花费这么长时间才能通过Windows 10上的串行端口与设备进行通信。我编写了两个小型测试应用程序以尝试查看导致其如此缓慢的原因。这是他们两个的代码:
''VB.NET code
Imports System.IO.Ports
Module Module1
Sub Main()
Dim port As New SerialPort("COM3", 921600, Parity.None, 8, 1)
port.Open()
port.DtrEnable = True
port.RtsEnable = True
Dim profiler As New Stopwatch
profiler.Start()
For i As Integer = 1 To 100
port.Write("1PA?" & vbCrLf)
port.ReadLine()
port.Write("TB" & vbCrLf)
port.ReadLine()
Next
profiler.Stop()
Console.WriteLine("Average: " & profiler.ElapsedMilliseconds / 100 & "ms")
Console.ReadKey()
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
和:
//C++ code
#include <iostream>
#include <string>
#include "boost/asio/io_service.hpp"
#include "boost/asio/serial_port.hpp"
#include "boost/asio/read_until.hpp"
#include "boost/asio/write.hpp"
#include "boost/asio/streambuf.hpp" …
Run Code Online (Sandbox Code Playgroud)