#include<osg/OperationThread>
//最长的一帧示例
class TestThread :public OpenThreads::Thread
{
public:
TestThread() :
_done(false),
_count(0)
{}
void block()
{
_operator.block();
}
virtual int cancel()
{
_operator.release();
_done = true;
while (isRunning())
OpenThreads::Thread::YieldCurrentThread();//让当前线程主动让出 CPU 时间片
return 0;
}
virtual void run()
{
do
{
std::cout << "(" << _count << ")";
++_count;
if (_count == 10)
{
_operator.release();
_operator.reset();
_operator.block();
}
microSleep(150000L);//微秒(百万分之一秒)
} while (!_done);
}
protected:
bool _done;
unsigned long _count;
OpenThreads::Block _operator;
};
//block()阻塞执行它的线程(不一定是定义它的线程)
int main()
{
TestThread t;
t.start();
t.block();
std::cout << "(Main)" << std::endl;
getchar();
t.cancel();
return 0;
}
OpenThreads::Block
于 2024-07-27 16:43:53 首次发布