c++ - 如何在 C++11 中创建/传递完成处理程序回调作为函数参数?

标签 c++ objective-c c++11 callback asynccallback

已经有大量示例/代码展示了如何在 C++11 中将函数作为回调传递给函数参数。然后回调被调用到一个单独的函数中,而不是调用到它的原始调用函数中。

比方说,我在 Objective-C

中有以下示例代码
- (void)calculateSizeWithCompletionBlock:(IPVWebImageCalculateSizeBlock)completionBlock {

    dispatch_async(self.ioQueue, ^{
        NSUInteger fileCount = 0;
        NSUInteger totalSize = 0;

        // Doing some time consuming task, that plays with some local(on this function scope) vars

        if (completionBlock) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completionBlock(fileCount, totalSize);
            });
        }
    });
}

- (void)doSomething {
    NSUInteger var1 = 0;
    NSUInteger var2 = 0;

    [self calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
        // Here, do things with fileCount, totalSize, var1, var2
        NSLog(@"fileCount: %lu, totalSize: %lu, var1: %lu, var2: %lu",(unsigned long)fileCount, (unsigned long)totalSize, (unsigned long)var1, (unsigned long)var2);
    }];
}

直接的问题是如何在 C++11 中重写上面的代码? 我的回调将被调用到调用函数中,以便它调用调用函数本地变量。我知道 C++11 的 Lambda、std::function、std::bind,但不确定如何实现。

如有任何帮助,我们将不胜感激。

最佳答案

thread_pool& get_threadpool();
void run_on_ui_thread( std::function<void()> );

std::future<void> calculateSizeWithCompletionBlock(
  std::function<void(int fileCount, int totalSize)> completion
) {
  get_threadpool.queue(
    [completion]{
      int fileCount = 0;
      int totalSize = 0;

      // Doing some time consuming task, that plays with some local(on this function scope) vars

      if (completion) {
        RunOnUIThread( [fileCount, totalSize, completion]{
          completion(fileCount, totalSize);
        });
      }
    }
  );
}

void doSomething() {
  int var1 = 0;
  int var2 = 0;

  calculateSizeWithCompletionBlock(
    [var1, var2](int fileCount, int totalSize) {
      // Here, do things with fileCount, totalSize, var1, var2
      std::cout <<
        "fileCount: " << fileCount <<
        ", totalSize: " << totalSize <<
        ", var1: " << var1 <<
        ", var2: " << var2 << "\n";
    }
  );
}

这是您的代码的粗略等价物。

我不包括 run_on_ui_threadget_threadpool,因为两者都取决于您的 C++ 程序运行的上下文。

这是我使用的thread_pool的唯一方法:

struct thread_pool {
  std::future<void> queue( std::function<void()> );
};

基本上,它采用类似函数的方式,并返回一个让您等待该任务完成的对象。

与 Objective-C 不同,C++ 可在大量不同的环境中运行。操作系统或其运行的任何其他环境的服务不是固定的。

例如,没有假设所有 C++ 代码都在交互式 UI 消息传输环境中运行。 run_on_ui_thread 隐含地假定了这一点,并且必须在编写时考虑到特定的 ui-thread-pump 库。

在 C++14 中,使用 move-into-lambda 可以稍微提高上面的一些代码的效率。特别是,

RunOnUIThread( [fileCount, totalSize, completion=std::move(completion)]{
  completion(fileCount, totalSize);
});

calculateSizeWithCompletionBlock 中,我们不知道复制completion 的成本是多少。在 C++ 中,您可以更多地按值访问对象,因此有时您必须显式地移动对象。从好的方面来说,与 Objective-C 相比,这减少了您必须执行的分配量。

关于c++ - 如何在 C++11 中创建/传递完成处理程序回调作为函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866120/

相关文章:

c++ - 带转换运算符的模板参数

c++ - 在 C++ 中及时求解 ODE 系统

c++ - OpenGL 中的抗锯齿

c++ - 使用 "time"作为 Cassandra 中的行键来存储网络数据包

iphone - IOS - ObjC 谷歌地图 API key

iphone - 使用 UILocalNotification 播放应用程序包中没有的声音

ios - 在选择器中编写方法?

c++ - std::transform vector 的 vector

c++ - 如何修复模板内的错误重构 decltype

c++ - 线程中的异常