C++ 防止WxWidget Boost线程关闭

C++ 防止WxWidget Boost线程关闭,c++,multithreading,boost,wxwidgets,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. */

以下函数是在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%。我试着让
睡眠(1)在while循环中,但没有任何区别,我不确定这将如何影响传输。

看起来您需要一个条件变量:只要在希望线程关闭时通知它。我认为这可能工作得很好。。如果我能想出如何通知线程我想关闭它。我必须将
data\u ready
设置为
true
,但如何从创建变量的线程外部执行此操作?要修改线程外部的变量,只需在启动外部线程时传递一个指向变量的指针。注意,您应该确保变量类型是原子的,否则您可能会有奇怪的行为。不过,您应该考虑使用条件变量,因为这不会像您忙等待的解决方案那样浪费CPU。