小编Jer*_*rry的帖子

为什么作为 lambda 函数发送参数不起作用,而作为普通函数指针发送却起作用

我一直在研究 libcurl 库,我需要为 API 提供一个回调函数,说明它应该如何处理接收到的数据。我尝试将这个回调函数作为 lambda 提供给它,它给了我访问冲突错误,而当我在其他地方定义函数后给出相同的函数作为函数指针时,它工作得很好!我想知道两者之间有什么区别,因为我认为它们是同一件事。

以下部分使用的代码来自https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html,这是libcurl的文档。

这是我向其发送 lambda 函数的代码(这会产生访问冲突错误):

int main() {
    // Irrelevant initial code...

    struct memory {
        char* response;
        size_t size;
    } chunk = { 0 };
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void*) &chunk);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, // I define a lambda function for the callback...
        [](void* data, size_t size, size_t nmemb, void* chunk) -> size_t {
            cout << "This function will never get called.. :(" << endl;
            size_t realSize = size * nmemb;
            memory* mem = (memory*) …
Run Code Online (Sandbox Code Playgroud)

c++ debugging curl libcurl

6
推荐指数
1
解决办法
146
查看次数

如何将包含对象的互斥体从一个函数传输到另一个函数?

#include <mutex>

class ConcurrentQueue {
    // This class contains a queue and contains functions push to the queue and pop from the queue done in a thread safe manner.
    std::mutex m;
};

class Producer {
    // This class contains several methods which take some ConcurrentQueue objects and then schedule tasks onto it.
public:
    void func(ConcurrentQueue a) {}
};

class Consumer {
    // This class contains several methods which take the same ConcurrentQueue objects and then remove the tasks and complete …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading design-patterns

1
推荐指数
1
解决办法
174
查看次数

标签 统计

c++ ×2

curl ×1

debugging ×1

design-patterns ×1

libcurl ×1

multithreading ×1