|
楼主 |
发表于 2009-10-19 19:51:28
|
显示全部楼层
本帖最后由 woshijiameizhou 于 2009-10-19 20:19 编辑
我在osg的while循环之前用_beginthread() 创建一个线程去获得方向盘等外设的采集的数据,然后更新控制小车的一些参数。如果不创建这个线程,则osg的渲染正常。后来我改为从OpenThreads::Thread派生一个线程类,在这个类的run()中采集外设数据,类的定义如下:
1# woshijiameizhou
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <OpenThreads/Thread>
#include <hysics/Car.h>
#include "USBRacingWheel.hpp"
class MyThread : public OpenThreads::Thread
{
public:
MyThread(osgVehicle:hysics::Car* car,USBRacingWheel &USB) :_done(false)
{
m_car = car;
usb = USB;
};
virtual ~MyThread() {};
virtual void run()
{
float engineForce = 0.f;
float breakForce = 1.0;
WheelInfo info;
while(!_done)
{
info = usb.getEventsInputData();
m_car->SetTurn(info.wheelAngle);
m_car->SetEngineForce(info.wheelEngine * info.gearPara);
m_car->SetBrake(info.wheelBrake);
OpenThreads::Thread::microSleep(1);
}
};
void stopWorking() {_done = true; };
protected:
bool _done;
osgVehicle::Physics::Car* m_car;
USBRacingWheel usb;
};
#endif
在osg的while循环之前new了这个派生的线程的一个对象,并启动了它(如下代码),可是程序好像就停在了这个线程类的run()里面,不会执行到osg的那个while循环。大家或者能不能谈一下怎么用这个OpenThreads::Thread,谢谢!
USBRacingWheel USB;
MyThread* updateVehicleInfoThread = NULL;
if(USB.IsOK())
{
updateVehicleInfoThread = new MyThread(driverCar,USB);
updateVehicleInfoThread->Init();
updateVehicleInfoThread->start();
updateVehicleInfoThread->run();
} |
|