阻止WxWidget Boost线程关闭

时间:2011-11-27 03:01:26

标签: c++ multithreading boost wxwidgets

在boost线程中创建以下函数。在控制台应用程序中它的工作原理如下。

void function()
{
    /* snip */
    try
    {
        /* Do stuff, in this case a P2P transfer is started using an external library
           as long as this thread is active, the P2P transfer will be too. */

        //Prevent thread from closing
        char a;
        std::cin >> a;
    }
    catch (std::exception& e)
    {
        /* snip */
    }
    return 0;
}

这会阻止线程在用户输入内容之前关闭。最终,我想要的是:

void function()
{
    int x = 1;
    /* snip */
    try
    {
        /* Do stuff, in this case a P2P transfer is started using an external library
           as long as this thread is active, the P2P transfer will be too. */

        //Prevent thread from closing
        while(x = 1)
        {
            //Do nothing until the user stops the transfer (x becomes 0 when the user hits stop)
        }
    }
    catch (std::exception& e)
    {
        /* snip */
    }
    return 0;
}

但这并不好...... CPU达到100%。我尝试将sleep(1);置于while循环中,但它没有任何区别,我不确定这将如何影响传输。

0 个答案:

没有答案