c++ - 我如何在后台的 dll 中使用 muti boost 线程

标签 c++ multithreading visual-c++ boost timer

后面的部分是我的程序,但它并不像我期望的那样工作。我想要主窗口程序在 dll 中调用函数“MyDllIniSys”,让 dll 渲染窗口每可能 32 微秒,直到主窗口程序设置“bIAutoRender”不等于 1。 所以我希望函数“MyDllIniSys”启动线程,并立即返回。 但是在我所做的事情中,程序无法运行,因为如果线程启动,它将永远不会返回。 我怎样才能得到它,有人请。帮助。 非常感谢

static void renderOneFrame(const boost::system::error_code& /*e*/,
    boost::asio::deadline_timer* t, int* iNeedAutoRender)
{


    //call Who use this DLL, let it refresh the window
    if(OnRefreshEvent)
    {
        OnRefreshEvent();
    }

    if(*iNeedAutoRender == 1)
    {
        t->expires_at(t->expires_at() + boost::posix_time::microseconds(iIRenderMicroSenconds));
        t->async_wait(boost::bind(renderOneFrame,
                boost::asio::placeholders::error, t, iNeedAutoRender));
    }

}

EXTERN_C MYDLLAPI INT MyDllIniSys(INT  WindowWidth,INT  WindowHeight)
{
    COgreRenderLoader myLoader;
    myLoader.IniOgre(externalWindowHandle,WindowWidth,WindowHeight);

    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::microseconds(iIRenderMicroSenconds));

    t.async_wait(boost::bind(renderOneFrame,
            boost::asio::placeholders::error, &t,&bIAutoRender));

    boost::thread thread1(boost::bind(&boost::asio::io_service::run, &io));
    //io.run();
    thread1.join();
    //thread1.start_thread();

    return 1;
}

最佳答案

调用 thread1.join() 将阻塞,直到 thread1 完成执行。将其关闭,您的函数将启动线程并立即返回。

线程将继续,即使 thread1 对象超出范围,as you can see from this question .

关于c++ - 我如何在后台的 dll 中使用 muti boost 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939883/

相关文章:

c# - 在新线程上使用 ObservableCollection

c++ - 如何读取 C++ 中的 ini 文件?

c++ - g++ 不输出任何错误

c - pthreads:等到 read() 返回值 > 0

c++ - 在 vector 中使用 std::filesystem::path 时双重释放

java - Java中的线程中断

delphi - Visual C++ 是开始可视化编程的好选择吗?

c++ - 如何在 C++ 中使用来自 dll 的结构?

c++ - 使用 C++ 和 opencv 在图像中进行色彩平衡

c++ - 如何说服控制台上的系统日志输出包含时间戳?