我一直在研究 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) #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)