在 boost 线程上调用抽象函数,不会在 interrupt_point 中断

Posted

技术标签:

【中文标题】在 boost 线程上调用抽象函数,不会在 interrupt_point 中断【英文标题】:Calling abstract function on boost thread, does not interrupt at interruption_point 【发布时间】:2011-09-02 18:08:22 【问题描述】:

我创建了一个抽象基类以允许多个任务的实现,可以通过 MFC 对话框进行一般调用。如果用户单击取消,此任务需要能够被中断。

abstract_dll.h:

class abstract_dll

public:
    virtual void my_task(CFeedback *fb)=0;

CFeedback 是一个抽象类,用于控制用户反馈(即进度条)

concrete_dll.h:

class concrete_dll 

    virtual void my_task(CFeedback *fb)
    
        //do some work
        //step progress bar
        boost::this_thread::interruption_point();

        //do some work
        //step progress bar
        boost::this_thread::interruption_point();
    


extern "C" abstract_dll* get_class()  return new concrete_dll(); 

现在在 MFC 对话框中,我加载了适当的具体_dll 并初始化我的 abstract_dll *dll = module->get_class();

然后启动一个新的boost::thread,它调用dll->my_task(fb);

然后当我打电话给thread.interrupt()。线程永远不会被中断,它永远不会在我的中断点除外。我已经跟踪了线程 ID,它是相同的,直到我们在具体_dll 实现中,然后我只得到 0x0000 的线程 ID。

有什么想法吗? PS。上面的代码只是我所拥有的伪代码。我的实际代码编译并运行,我就是无法让它中断。

【问题讨论】:

【参考方案1】:

我想你的问题的答案就在这里:

http://lists.boost.org/boost-users/2010/01/55171.php

简而言之:您需要在项目和 DLL 中链接到 boost:threads 的 DLL 版本。只需输入:

#define BOOST_THREAD_USE_DLL

<boost/thread.hpp> 包含之前(或在项目属性中)

BR

【讨论】:

谢谢!我们在当天晚些时候弄清楚了多个数据结构。但是我们没有弄清楚 BOOST_THREAD_USE_DLL,但是我们通过将中断放在 CFeedBack 中解决了这个问题,这样它就会回到主线程池结构。再次感谢。

以上是关于在 boost 线程上调用抽象函数,不会在 interrupt_point 中断的主要内容,如果未能解决你的问题,请参考以下文章

如何通过boost线程调用函数指针

C ++ 0x中的线程析构函数与boost

C ++ 0x中的线程析构函数与boost

boost::python - 如何从 C++ 在自己的线程中调用 python 函数?

从多个线程调用 boost::asio::io_service 运行函数

从 boost 线程在主线程上运行一个函数并将参数传递给该函数