1
void workerFunc()  //first thread
{  
    for (int x=0;x<30;x++) //count up to 29
    {
        cout << x << endl;
        Sleep(1000); //one sec delay
    }
}    

void test()   //second thread
{
    for (int y=30;y<99;y++)  //count up to 98
    {
        cout <<'\t'<< y << endl;        
        Sleep (1000); //delay one sec
    }
}

int main(int argc, char* argv[])   
{  
    cout << "Main started " << endl; 
    boost::thread workerThread(workerFunc);    //start the 1st thread
    boost::thread t1(test); //start the second thread
    cin.get();     
    return 0;  
}

こんにちは、y=35 のスレッドの場合、スレッド内でスレッドtest()を一時停止/中断したいです 。workerFunc()test()

どうすればこれを達成できますか?

4

1 に答える 1

1

使用できますboost::thread::interrupt。ターゲット スレッドは、割り込みの準備が整ったときに、割り込みポイント関数のいずれかを定期的に呼び出す必要があります。別のスレッドが を呼び出すとboost::thread::interrupt、この関数は例外をスローしboost::thread_interruptedます。スレッドが状態にある場合、それは中断できませんが、によって中断できる関数のいずれかを呼び出す必要がある場合は、classboost::thread::interruptで中断を一時的に無効にするだけです。disable_interruption

あなたの場合、Sleep呼び出しをboost::this_thread::sleep呼び出しに置き換えるだけです。

于 2012-10-29T06:27:27.027 に答える