QtConcurrent :: run可以与对象的智能指针一起使用吗?

时间:2019-02-05 22:07:05

标签: c++ multithreading qt smart-pointers qtconcurrent

Qt Documentation指出java.lang.NoSuchMethodError: No static method with(Landroid/content/Context;)Lcom/squareup/picasso/Picasso; in class Lcom/squareup/picasso/Picasso; or its super classes (declaration of 'com.squareup.picasso.Picasso' appears in /data/app/app.package.here/base.apk:classes3.dex) at com.mercadopago.android.px.internal.features.paymentresult.components.IconRenderer.renderIconFromResource(IconRenderer.java:64) at com.mercadopago.android.px.internal.features.paymentresult.components.IconRenderer.render(IconRenderer.java:33) at com.mercadopago.android.px.internal.features.paymentresult.components.IconRenderer.render(IconRenderer.java:18) at com.mercadopago.android.px.internal.view.Renderer.render(Renderer.java:35) at com.mercadopago.android.px.internal.features.paymentresult.components.HeaderRenderer.render(HeaderRenderer.java:42) at com.mercadopago.android.px.internal.features.paymentresult.components.HeaderRenderer.render(HeaderRenderer.java:18) at com.mercadopago.android.px.internal.view.Renderer.render(Renderer.java:35) at com.mercadopago.android.px.internal.features.paymentresult.components.PaymentResultRenderer.render(PaymentResultRenderer.java:24) at com.mercadopago.android.px.internal.features.paymentresult.components.PaymentResultRenderer.render(PaymentResultRenderer.java:10) at com.mercadopago.android.px.internal.view.Renderer.render(Renderer.java:35) at com.mercadopago.android.px.internal.view.ComponentManager.render(ComponentManager.java:30) at com.mercadopago.android.px.internal.view.ComponentManager.onProps(ComponentManager.java:64) at com.mercadopago.android.px.internal.features.paymentresult.PaymentResultPropsMutator.notifyPropsChanged(PaymentResultPropsMutator.java:60) at com.mercadopago.android.px.internal.features.paymentresult.PaymentResultPresenter.checkGetInstructions(PaymentResultPresenter.java:99) at com.mercadopago.android.px.internal.features.paymentresult.PaymentResultPresenter.onValidStart(PaymentResultPresenter.java:72) at com.mercadopago.android.px.internal.features.paymentresult.PaymentResultPresenter.initialize(PaymentResultPresenter.java:52) at com.mercadopago.android.px.internal.features.paymentresult.PaymentResultActivity.onResume(PaymentResultActivity.java:150) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) at android.app.Activity.performResume(Activity.java:6783) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3406) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)``` 可以通过将指针作为第一个参数传递给对象来用于在另一个线程中运行成员函数。但是,如果在这种情况下可以使用智能指针,我将找不到任何信息。具体来说,我想将其与QtConcurrent::run一起使用。

1 个答案:

答案 0 :(得分:2)

由于没有可用的匹配重载,因此不可能将智能指针传递给QtConcurrent :: run。

我建议使用lambdas解决方案:

std::shared_ptr<Obj> obj_ptr(new Obj());
QtConcurrent::run([obj_ptr](){ obj_ptr->func(); });

由于shared_ptr的内部引用计数器,您不必担心智能指针所拥有的对象的生存时间,因为您在lambda函数中捕获了副本。

另一种解决方案是传递原始指针:

std::shared_ptr<Obj> obj_ptr(new Obj());
QtConcurrent::run(obj_ptr.get(), &Obj::func);

但这很有风险,因为您必须担心对象的寿命。

最后,我更喜欢第一种方法,因为它更安全。