我正在开发用于OCR的C ++库。当我需要在用户发送取消信号(只是一个布尔函数指针)后立即(<= 0.4秒)添加功能取消/停止其工作时出现问题,而没有任何内存泄漏错误。 现在,我选择解决方案,在通过剖析消耗大约1%性能的每个函数之后放置很多检查点。然后,我将抛出一个异常并在最高调用者中捕获该异常,并使用智能指针来防止我的库内存泄漏。为此目的需要20个检查点,因为很多检查点都在循环内。 但是我看到这种解决方案很糟糕,因为在大多数情况下它不能确保目标为0.4秒。而且防止内存泄漏的努力也非常高。另外,当继续开发库时,很难维持这种逻辑。
您能为我提出更好的解决方案来解决此问题吗? 非常感谢您!
我的情况是像下面这样的演示:
#include "OCRSetting.h"
#include "OCRDocument.h"
bool is_cancel(false);
bool IsCancel() {
return is_cancel;
}
int main() {
// OCR setting
OCRSetting setting;
// Create document
OCRDocument document(image_path, setting);
document.SetCancelMethod(IsCancel);
// This is a action take a lot of time, when this action happen, the controller
// thread can change the result of IsCancel to true and the requirement is that
// methid Export must return immedidately with response time smaller than 0.4 seconds
// Target CPU frequency is 1.33 GHz
int status = document.Export();
return status;
}