const SocketChannelPtr& channel = getChannel(connfd);
.......
//lambda函数定义:
channel->onread = [this, &channel](Buffer* buf) {
HttpClientContext* ctx = channel->getContext<HttpClientContext>();......
};
调用:
private:
static void on_read(hio_t* io, void* data, int readbytes) {
Channel* channel = (Channel*)hio_context(io);
if (channel && channel->onread) {
Buffer buf(data, readbytes);
channel->onread(&buf);
}
}
#include <iostream>
#include <thread>
#include <functional> // 为了 std::ref
void modifyValue(int& x) {
x = 42;
}
int main() {
int value = 0;
std::thread t(modifyValue, std::ref(value)); // 注意 std::ref 的使用//if (t.joinable()) {t.join()}
t.join();
std::cout << "Value is now: " << value << std::endl; // 输出 42
return 0;
}