我如何在后台的dll中使用多boost线程

how can I use muti boost thread in a dll in background

本文关键字:boost 线程 dll 后台      更新时间:2023-10-16

后部分是我的程序,但它不像我期望的那样工作。我希望主窗口程序在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对象超出了作用域,从这个问题中可以看出